I'm confused what the object[foo]
term is referring to. Any hints? I know that bar['unique_prop']
and bar.unique_prop
refers to 2
我很困惑对象[foo]术语指的是什么。任何提示?我知道bar ['unique_prop']和bar.unique_prop指的是2
var foo = {unique_prop: 1};
var bar = {unique_prop: 2};
var object = {};
object[foo] = 'value';
alert(object[bar]);
jsfiddle上的代码
3 个解决方案
#1
5
This:
var foo = 'abc';
object[foo]
is equivalent to:
相当于:
object.abc
However this:
var foo = {unique_prop: 1};
object[foo] = 'value';
Doesn't have much sense (object property names cannot be object literals). JS engine will use foo.toString()
(returning e.g. "[object Object]"
), so in fact you are doing this:
没有多大意义(对象属性名称不能是对象文字)。 JS引擎将使用foo.toString()(返回例如“[object Object]”),所以实际上你这样做:
var foo = {unique_prop: 1};
object["[object Object]"] = 'value';
which is clearly a bug or a misunderstanding. This behaviour is explained in Member Operators on MDN:
这显然是一个错误或误解。 MDN上的成员操作员解释了此行为:
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
属性名称必须是字符串。这意味着非字符串对象不能用作对象中的键。任何非字符串对象(包括数字)都通过toString方法被类型化为字符串。
You can however use:
但是你可以使用:
object[foo.unique_prop] = 'value';
which is equivalent to:
这相当于:
object[1] = 'value';
#2
2
It is the same as object.whatever_x_is
where x
is foo.toString()
which will be the same as bar.toString()
since (unless overridden) calling toString
on a generic object will generate a generic "is an object" string such as "[object Object]"
它与object.whatever_x_is相同,其中x是foo.toString(),它与bar.toString()相同,因为(除非被覆盖)在泛型对象上调用toString将生成泛型“is a object”字符串,例如“[object Object]”
#3
1
object[foo] = 'value';
attempts to use an object as a member name, this causes a .toString()
call so;
尝试使用对象作为成员名称,这会导致.toString()调用;
'value'
is assigned to object["[object Object]"]
, when you attempt to read alert(object[bar]);
the same toString()
occurs as bar
is also an object, so you reference object["[object Object]"]
once again and get back 'value
'.
当您尝试读取alert(object [bar])时,'value'被赋值给object [“[object Object]”];同样的toString()发生,因为bar也是一个对象,所以再次引用对象[“[object Object]”]并返回'value'。
#1
5
This:
var foo = 'abc';
object[foo]
is equivalent to:
相当于:
object.abc
However this:
var foo = {unique_prop: 1};
object[foo] = 'value';
Doesn't have much sense (object property names cannot be object literals). JS engine will use foo.toString()
(returning e.g. "[object Object]"
), so in fact you are doing this:
没有多大意义(对象属性名称不能是对象文字)。 JS引擎将使用foo.toString()(返回例如“[object Object]”),所以实际上你这样做:
var foo = {unique_prop: 1};
object["[object Object]"] = 'value';
which is clearly a bug or a misunderstanding. This behaviour is explained in Member Operators on MDN:
这显然是一个错误或误解。 MDN上的成员操作员解释了此行为:
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
属性名称必须是字符串。这意味着非字符串对象不能用作对象中的键。任何非字符串对象(包括数字)都通过toString方法被类型化为字符串。
You can however use:
但是你可以使用:
object[foo.unique_prop] = 'value';
which is equivalent to:
这相当于:
object[1] = 'value';
#2
2
It is the same as object.whatever_x_is
where x
is foo.toString()
which will be the same as bar.toString()
since (unless overridden) calling toString
on a generic object will generate a generic "is an object" string such as "[object Object]"
它与object.whatever_x_is相同,其中x是foo.toString(),它与bar.toString()相同,因为(除非被覆盖)在泛型对象上调用toString将生成泛型“is a object”字符串,例如“[object Object]”
#3
1
object[foo] = 'value';
attempts to use an object as a member name, this causes a .toString()
call so;
尝试使用对象作为成员名称,这会导致.toString()调用;
'value'
is assigned to object["[object Object]"]
, when you attempt to read alert(object[bar]);
the same toString()
occurs as bar
is also an object, so you reference object["[object Object]"]
once again and get back 'value
'.
当您尝试读取alert(object [bar])时,'value'被赋值给object [“[object Object]”];同样的toString()发生,因为bar也是一个对象,所以再次引用对象[“[object Object]”]并返回'value'。