var a={};
var b={key:1};
var c={key:2};
a[b]=3;
a[c] =4;
console.log(a[b]);
I personally think that the content of a[b] should be 3. The above var b and var c are useless, but the actual answer is a[c] is 4, so I can’t understand it. What is the explanation of objects in js? How is this occupied? I hope someone can explain it, thank you very much. . . . . . .
I guess your question is why the value is overwritten, right?
In Javascript, when accesses object properties through
[]
, the expression in square brackets will be evaluated and converted into a string , call itstoString
method.So:
So
b
andc
are converted into the same string[object Object]
. So it will be overwritten if assigned again.Because b.toString() and c.toString() are both [Object object], so they are the same