Object and primitive type equality

后端 未结 5 1778
庸人自扰
庸人自扰 2021-01-16 04:13

I know that identical objects are not equal, i.e:

var obj = { name: \"Value\" };
var obj2 = { name: \"Value\" };

console.log(\"obj equals o         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 04:39

    This seems to really be a question about === so let's look at the Strict Equality Comparison Algorithm, in which point 7 says

    Return true if x and y refer to the same object. Otherwise, return false.

    So what does it mean to be "the same object"? It means they don't just look like eachother, but are at the same place in memory too. This means that the only time when an Object is === to an Object is when they're the same thing.

    var a = {},
        b = {}, // identical to `a`
        c = a;  // same as `a`
    a === b; // false
    a === c; // true
    b === c; // false
    

提交回复
热议问题