var a = new Object;
var b = new Object;
var c = new Object;
c[a] = a;
c[b] = b;
console.log(c[a] === a);
I tested the code above and get false
. If I try console.log(c[a] === b)
, then true
is printed.
Why?
javascript
var a = new Object;
var b = new Object;
var c = new Object;
c[a] = a;
c[b] = b;
console.log(c[a] === a);
I tested the code above and get false
. If I try console.log(c[a] === b)
, then true
is printed.
Why?
Best Answer
The problem here has to do with how an
Object
's keys are set. From MDN:An object's values can be accessed (via the appropriate key) in three ways:
When using bracket notation, you need to mind the gap...between the brackets! Objects set their keys and values using the
toString
method, unless they're passed a string (then there's no point intoString
). When using the dot notation, they use.key
as the key.Let's look at your case: