问题描述
为什么以下行在Javascript中返回 false
:
Why does the following line return false
in Javascript:
[[1,2,3], [1,2,4]].includes([1,2,3]);
背后的基本逻辑是什么?
What is the underlying logic behind that?
推荐答案
包括
使用等式算法。 (如中所述)。搜索对象时(数组也是对象),它只匹配对同一对象的引用。
includes
compares using SameValueZero equality algorithm. (As mentioned in developer.mozilla.org). When searching for objects (array is object as well), it will match only references to the same object.
此外,Javascript数组是对象,你不能简单地使用等于运算符 ==
,以了解这些对象的内容是否相同。等于运算符只会测试两个对象实际上是否完全相同(例如 myObjVariable == myObjVariable
,适用于 null
和 undefined
。)
Additionally, Javascript arrays are objects and you can't simply use the equality operator ==
to understand if the content of those objects is the same. The equality operator will only test if two object are actually exactly the same instance (e.g. myObjVariable==myObjVariable
, works for null
and undefined
too).
这篇关于为什么我不能使用Array#includes作为嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!