In Javascript, when accesses object properties through [], the expression in square brackets will be evaluated and converted into a string , calling its toString method. So:
var a = {};
b={key:'b'};
console.log(b.toString()); // [object Object]
a[b]=123;
console.log(typeof Object.keys(a)[0]); // string, 属性名 b 转换成了字符串.
So b and c are converted into the same string [object Object]. So it will be overwritten if assigned again.
In Javascript, when accesses object properties through
[]
, the expression in square brackets will be evaluated and converted into a string , calling itstoString
method.So:
So
b
andc
are converted into the same string[object Object]
. So it will be overwritten if assigned again.a[b]=123; After this step, print console.log(a); you will suddenly understand
If you treat object
b
as an attribute ofa
, thetoString()
method of objectb
will be called first.So,