[removed] Equality Comparison between two Object/ Array

前端 未结 3 393
忘掉有多难
忘掉有多难 2021-01-27 01:05

Let us guess two objects with same property:

var x = {a : \'some\'},
      y = {a: \'some\'};

output:

x == y; and x

相关标签:
3条回答
  • 2021-01-27 01:43

    All above comparison give true.

    Right. You've specifically set p and q so they refer to the same object.

    With object references, both == (if both sides are object references) and === will check to see if the references are pointing to the same object. If you have two identical, but separate, objects, both will always be false.

    So for example:

    var a = {}; // a points to an object
    var b = {}; // b points to a _different_ object
    console.log(a === b); // "false"
    console.log(a == b);  // "false"
    
    var c = {}; // c points to an object
    var d = c;  // d points to _the same_ object
    console.log(c === d); // "true"
    console.log(c == d);  // "true"
    

    The content of the objects is irrelevant, it's the identity of them that's being checked.

    Note that this is not true if you use == and only one side is an object reference (e.g., the other side is a number, a primitive string, undefined, etc.). In that case, the object reference will be asked to convert itself (to either a string or a number, depending on what the other thing is), and then the converted result will be used for the comparison. This can lead to surprising behavior (for instance, "2" == [[[[[2]]]]] is true because the array is asked to turn itself into a string, which it does via join [which will ask its element to convert itself to a string, and so on], and you end up with "2" on the right-hand side). So I typically prefer === ("strict equality" over == ("loose equality").

    0 讨论(0)
  • 2021-01-27 01:55

    In this cases, the variables point to two separate objects, thus not equal.

    var x = {a:'some'}, // one object with "a" = "some"
        y = {a:'some'}; // another object with "a" = "some"
    
    var p = [1,2,3],    // one array with 1,2 and 3
        q = [1,2,3];    // another array with 1,2 and 3
    

    But in this case, they point to the same object, thus equal

    var x = y = {a: 'some'};
    //is like:
    var x = {a:'some'}, // x points to an object
        y = x;          // y is given reference to whatever x is pointing at
    
    
    var p = q = [1,2,3];
    //is like:
    var p = [1,2,3],    // p points to an array
        q = p;          // q is given reference to whatever p is pointing at
    
    0 讨论(0)
  • 2021-01-27 01:56

    This happens because

    var p = [1, 2, 3],
    q = [1,2,3];
    

    creates two instances of arrays. Equality operators in JS, when comparing non-primitive data, only check if they are the same instance, don't do deep check of values or properties.

    With code

    var p = q = [1, 2, 3];
    

    you are creating ONE instance of the array and assign this instance to variables p and q. So, both variables store reference to the same instance, so equality operator will return true.

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