理解对象字面量
Understanding object literals
在练习几个示例时,我遇到了以下示例:
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object[foo] = 'value';
alert(object[bar]);
其中创建了两个对象 foo 和 bar。我不知道如何 alert(object[bar]);是 "value"。
foo 和 bar 之间的 link 是什么。
另外,稍微改变一下,输出结果就是 "undefined",如下例所示。
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object["foo"] = 'value';
alert(object[bar]);
默认情况下,[]
符号可以正确使用字符串,["some_property"]
和[some_property]
不一样吗?
使用方括号表示法时,方括号内的任何内容都将转换为字符串。然后该字符串用于查找名为同一事物的 属性。
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object[foo] = 'value';
// foo is an object, so it's automatically turned into the string "[object Object]"
// so the above code is equivalent to `object["[object Object]"] = 'value';`
alert(object[bar]);
// bar is also an object, so is converted into the same string
// the above code is also equivalent to `alert(object["[object Object]"]);` which of course accesses that same value
var blah = "not blah";
object.blah = 1;
object["blah"] = 1;
object[blah];
// a variable is used.
// therefore the value of that variable is what the assessor is looking for, not the name of the variable.
// so the above code is equivalent to `object["not blah"];`.
对象的键只能是字符串*,因此当您使用非字符串的值访问对象的 属性 时,它会转换为字符串。
在 ECMAScript 6 中你可以使用 Map,这类似于对象,但你可以使用任何值作为键。示例:
const foo = {unique_prop: 1}
const bar = {unique_prop: 2}
const map = new Map()
map.set(foo, 'value')
console.log(map.get(bar)) // undefined
* 在 ECMAScript 6 中,您还可以使用 symbols,但这与此处无关。
在练习几个示例时,我遇到了以下示例:
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object[foo] = 'value';
alert(object[bar]);
其中创建了两个对象 foo 和 bar。我不知道如何 alert(object[bar]);是 "value"。 foo 和 bar 之间的 link 是什么。
另外,稍微改变一下,输出结果就是 "undefined",如下例所示。
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object["foo"] = 'value';
alert(object[bar]);
默认情况下,[]
符号可以正确使用字符串,["some_property"]
和[some_property]
不一样吗?
使用方括号表示法时,方括号内的任何内容都将转换为字符串。然后该字符串用于查找名为同一事物的 属性。
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object[foo] = 'value';
// foo is an object, so it's automatically turned into the string "[object Object]"
// so the above code is equivalent to `object["[object Object]"] = 'value';`
alert(object[bar]);
// bar is also an object, so is converted into the same string
// the above code is also equivalent to `alert(object["[object Object]"]);` which of course accesses that same value
var blah = "not blah";
object.blah = 1;
object["blah"] = 1;
object[blah];
// a variable is used.
// therefore the value of that variable is what the assessor is looking for, not the name of the variable.
// so the above code is equivalent to `object["not blah"];`.
对象的键只能是字符串*,因此当您使用非字符串的值访问对象的 属性 时,它会转换为字符串。
在 ECMAScript 6 中你可以使用 Map,这类似于对象,但你可以使用任何值作为键。示例:
const foo = {unique_prop: 1}
const bar = {unique_prop: 2}
const map = new Map()
map.set(foo, 'value')
console.log(map.get(bar)) // undefined
* 在 ECMAScript 6 中,您还可以使用 symbols,但这与此处无关。