使用空数组时比较运算符JavaScript的奇怪行为 [英] Weird behavior of comparison operator JavaScript when using empty array
本文介绍了使用空数组时比较运算符JavaScript的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能解释一下奇怪的JavaScript行为吗?
Can you explain this weird JavaScript behavior?
首先:
[] === [] false
[] == [] false
为什么是假的?对象是相同的,因此它应该返回true。
Why false? The object are identical, thus it should return true.
第二名:
[] !== [] true
[] != [] true
再次,为什么是真的?对象是相同的。
Again, why true? the objects are identical.
推荐答案
它们并不相同。 对象标识由指向同一实例的两个操作数定义。
They're not identical. Object identity is defined by both operands pointing to the same instance.
var a = [],
b = [];
a == b; // false
a == a; // true
两个文字总是评估为两个不同的实例,这些实例不相等。如果您正在寻找结构等效性,请参阅如何比较JavaScript中的数组?。
Two literals always evaluate to two different instances, which are not considered equal. If you are looking for structural equivalence, see How to compare arrays in JavaScript?.
这篇关于使用空数组时比较运算符JavaScript的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文