[removed] Equality Comparison between two Object/ Array

前端 未结 3 391
忘掉有多难
忘掉有多难 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: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
    

提交回复
热议问题