javascript - javascript 中的两个对象/数组会巧合吗?

标签 javascript reference

假设我有数组 a。

var a = [1,2,3];

然后是数组b。

var b = [1,2,3];

a == b 是否有可能返回 true?即使是百万分之一。 (我知道通常 a == b 为假,但我想知道它是否有可能为真。)

最佳答案

规范的相关部分是11.9.3 The Abstract 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 the same as Type(y), then
    1. If Type(x) is Undefined, return true.
    2. If Type(x) is Null, return true.
    3. If Type(x) is Number, then
      1. If x is NaN, return false.
      2. If y is NaN, return false.
      3. If x is the same Number value as y, return true.
      4. If x is +0 and y is −0, return true.
      5. If x is −0 and y is +0, return true.
      6. Return false.
    4. 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.
    5. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
    6. Return true if x and y refer to the same object. Otherwise, return false.

(强调我的。)

所以你的问题的答案是否定的。 a == b 仅当它们实际上是同一个对象时才返回 true

关于javascript - javascript 中的两个对象/数组会巧合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127967/

相关文章:

c# - ReferenceEquals() 的合法使用

c++ - 在 C++ 中通过引用修改此操作的开销是多少?

javascript - 如何使立方体的内部颜色在 three.js 中可见?

javascript - React 渲染错误

javascript - 按 Enter 键 react 组件

javascript - 如何使用 JavaScript 在 PDF 中导出 SVG

javascript - jQuery noconflict 新版本同时保留旧的 jQuery

c# - 为什么字符串不比较引用?

c++ - 如何使用此 OpenMP 关键部分避免此原始指针?

c++ - 引用基类的成员 var 导致访问冲突