Why are two identical objects not equal to each other?

前端 未结 9 658
广开言路
广开言路 2020-11-22 05:43

Seems like the following code should return a true, but it returns false.

var a = {};
var b = {};

console.log(a==b); //returns false
console.log(a===b); //         


        
相关标签:
9条回答
  • 2020-11-22 05:55

    As from The Definitive Guide to Javascript.

    Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order.

    var o = {x:1}, p = {x:1};  // Two objects with the same properties
    o === p                    // => false: distinct objects are never equal 
    var a = [], b = [];        // Two distinct, empty arrays 
    a === b                    // => false: distinct arrays are never equal 

    Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types. Using this terminology, object values are references, and we say that objects are compared by reference: two object values are the same if and only if they refer to the same underlying object.

    var a = {};   // The variable a refers to an empty object. 
    var b = a;    // Now b refers to the same object. 
    b.property = 1;     // Mutate the object referred to by variable b. 
    a.property          // => 1: the change is also visible through variable a. 
    a === b       // => true: a and b refer to the same object, so they are equal. 

    If we want to compare two distinct objects we must compare their properties.

    0 讨论(0)
  • 2020-11-22 05:55
    use JSON.stringify(objname);
    
    var a = {name : "name1"};
    var b = {name : "name1"};
    
    var c = JSON.stringify(a);
    var d = JSON.stringify(b);
    
    c==d;
    //true
    
    0 讨论(0)
  • 2020-11-22 05:58

    How does this make sense?

    Because "equality" of object references, in terms of the == and === operators, is purely based on whether the references refer to the same object. This is clearly laid out in the abstract equality comparison algorithm (used by ==) and the strict equality comparison algorithm (used by ===).

    In your code, when you say a==b or a===b, you're not comparing the objects, you're comparing the references in a and b to see if they refer to the same object. This is just how JavaScript is defined, and in line with how equality operators in many (but not all) other languages are defined (Java, C# [unless the operator is overridden, as it is for string], and C++ for instance).

    JavaScript has no inbuilt concept of equivalence, a comparison between objects that indicates whether they're equivalent (e.g., have the same properties with the same values, like Java's Object#equals). You can define one within your own codebase, but there's nothing intrinsic that defines it.

    0 讨论(0)
  • 2020-11-22 05:59

    How does this make sense?

    Imagine these two objects:

    var a = { someVar: 5 }
    var b = { another: 'hi' }
    

    Now if you did a === b, you would intuitively think it should be false (which is correct). But do you think it is false because the objects contain different keys, or because they are different objects? Next imagine removing the keys from each object:

    delete a.someVar
    delete b.another
    

    Both are now empty objects, but the equality check will still be exactly the same, because you are still comparing whether or not a and b are the same object (not whether they contain the same keys and values).

    0 讨论(0)
  • 2020-11-22 06:01

    ===, the strictly equal operator for objects checks for identity.

    Two objects are strictly equal if they refer to the same Object.

    Those are two different objects, so they differ.

    Think of two empty pages of paper. Their attributes are the same, yet they are not the same thing. If you write something on one of them, the other wouldn't change.

    0 讨论(0)
  • 2020-11-22 06:02

    The only difference between regular (==) and strict (===) equality is that the strict equality operator disables type conversion. Since you're already comparing two variables of the same type, the kind of equality operator you use doesn't matter.

    Regardless of whether you use regular or strict equality, object comparisons only evaluate to true if you compare the same exact object.

    That is, given var a = {}, b = a, c = {};, a == a, a == b, but a != c.

    Two different objects (even if they both have zero or the same exact properties) will never compare equally. If you need to compare the equality of two object's properties, this question has very helpful answers.

    0 讨论(0)
提交回复
热议问题