Why is object[key] not equal to key, if key is an object, in JavaScript?

后端 未结 3 1088
我在风中等你
我在风中等你 2020-12-09 08:52
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 fa

3条回答
  •  天命终不由人
    2020-12-09 09:05

    I experimented with it a lit bit and @royhowie might be right. As you can see in this implementation I switched the order of the assignments and then c[a] == a gave a true.

    var a = new Object;
    var b = new Object;
    var c = new Object;
    
    //I switched the following lines of code
    c[b]=b; 
    c[a]=a;
    
    console.log(c[a]===a);
    

    Output: true

提交回复
热议问题