标题几乎说明了一切,但在这里写出来:
b = [1, 2, 3, 4];
c = [...b];
b === c; //false
为什么?
最佳答案
正则数组是怎么来的identity / strict equality comparison作品。请记住,数组是对象:
The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same Number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
- If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false.
NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.
...
没有影响。如果我们为两者分配相同的文字,我们可以看到:
b = [1, 2, 3, 4];
c = [1, 2, 3, 4];
b === c; //false
这是因为每个 []
都会创建一个新数组,即使它在其中使用了一个 spread。
关于javascript - 如果 b = [1, 2, 3, 4],并且 c = [...b],为什么 b 不等于 c?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243158/