[removed] Equality Comparison between two Object/ Array

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

提交回复
热议问题