javascript - 如果 b = [1, 2, 3, 4],并且 c = [...b],为什么 b 不等于 c?

标签 javascript arrays spread-syntax

标题几乎说明了一切,但在这里写出来:

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:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. 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.
  5. 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.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. 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/

相关文章:

javascript - 手动引导/初始化时访问 Angular 服务

javascript - 如何使用 javascript 从 firebase 检索数据

TypeScript 2.8.3 类型必须具有返回迭代器的 Symbol.iterator 方法

typescript - 使用扩展语法时如何删除对象的属性以创建对象的新实例?

javascript - MongoDB C# 以及如何使用 javascript 从客户端更新

javascript - 收缩箱

php - 根据值将多维数组拆分为一组数组的有效方法是什么?

C 在我的数组中得到错误的数字

jquery - 如何使用数组索引对无序列表进行排序

reactjs - 用 JSX 传播属性覆盖 JSX 属性