JavaScript 相等传递性很奇怪
我一直在阅读 Douglas Crockford 的 JavaScript:优秀部分,我遇到了这个对我来说没有意义的奇怪例子:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == undefined // false
false == null // false
null == undefined // true
作者还继续提到“永远不要使用 ==
和 !=
。相反,请始终使用 ===
和 !==
”。但他并没有解释为什么会出现上述行为?所以我的问题是,为什么上面的结果是这样的? JavaScript 中不考虑传递性吗?
I've been reading Douglas Crockford's JavaScript: The Good Parts, and I came across this weird example that doesn't make sense to me:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == undefined // false
false == null // false
null == undefined // true
The author also goes on to mention "to never use ==
and !=
. Instead, always use ===
and !==
". However, he doesn't explain why the above behavior is exhibited? So my question is, why are the above results as they are? Isn't transitivity considered in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
左侧是一个空字符串,右侧是一个包含一个字符的字符串。它们是错误的,因为它正在两个不同的字符串之间进行比较(感谢 Niall)。
因此,为什么这个是 true,因为
0
是 falsy 而空字符串是 falsy。这个有点棘手。规范规定,如果操作数是字符串和数字,则将字符串强制转换为数字。
'0'
变为0
。感谢smfoote。undefined
值在 JavaScript 中很特殊,不等于除null
之外的任何值。然而,它是虚假的。同样,
null
很特殊。它仅等于未定义
。它也是假的。null
和undefined
类似,但不一样。null
表示什么都没有,而undefined
是未设置或不存在的变量的值。他们的价值观被认为是平等的,这是有道理的。如果你真的很困惑,请检查这个...
仅由空格组成的字符串被视为等于 0。Douglas
Crockford 提出了很多建议,但你不必将它们视为福音。 :)
TJ Crowder 提出了学习ECMAScript 语言规范 了解这些平等测试背后的整个故事。
进一步阅读?
规范。
yolpo(关于虚假值)
The left hand side is an empty string, and the right hand side is a string with one character. They are false because it is making a comparison between two un identical strings (thanks Niall).
Hence, why this one is true, because
0
is falsy and the empty string is falsy.This one is a bit trickier. The spec states that if the operands are a string and a number, then coerce the string to number.
'0'
becomes0
. Thanks smfoote.The value
undefined
is special in JavaScript and is not equal to anything else exceptnull
. However, it is falsy.Again,
null
is special. It is only equal toundefined
. It is also falsy.null
andundefined
are similar, but not the same.null
means nothing, whilstundefined
is the value for a variable not set or not existing. It would kind of make sense that their values would be considered equal.If you want to be really confused, check this...
A string consisting only of whitespace is considered equal to 0.
Douglas Crockford makes a lot of recommendations, but you don't have to take them as gospel. :)
T.J. Crowder makes an excellent suggestion of studying the ECMAScript Language Specification to know the whole story behind these equality tests.
Further Reading?
The spec.
yolpo (on falsy values)
这个问题的答案与 JavaScript 如何处理强制有关。在
==
的情况下,字符串被强制转换为数字。因此:'' == '0'
等价于'' === '0'
(两者都是字符串,因此不需要强制转换)。0 == ''
相当于0 === 0
,因为字符串''
变成了数字0
(math.abs('') === 0
)。出于同样的原因,
0 == '0'
相当于0 === 0
。false == undefined
相当于0 === undefined
因为当类型不匹配时,JavaScript 会将布尔值强制转换为数字false == null
出于同样的原因,相当于0 === null
。null == undefined
是 true,因为规范是这么说的。感谢您提出这个问题。经过研究,我对
==
的理解要好得多。The answer to this question has to do with how JavaScript handles coercion. In the case of
==
, strings are coerced to be numbers. Therefore:'' == '0'
is equivalent to'' === '0'
(both are strings, so no coercion is necessary).0 == ''
is equivalent to0 === 0
because the string''
becomes the number0
(math.abs('') === 0
).0 == '0'
is equivalent to0 === 0
for the same reason.false == undefined
is equivalent to0 === undefined
because JavaScript coerces booleans to be numbers when types don't matchfalse == null
is equivalent to0 === null
for the same reason.null == undefined
is true because the spec says so.Thanks for asking this question. My understanding of
==
is much better for having researched it.实际上,您可以编写一个行为与
==
完全相同的 JavaScript 函数,这应该能让您深入了解它的行为方式。为了向您展示我在这里的意思是该函数:
如您所见
==
有很多复杂的类型转换逻辑。因此,很难预测你会得到什么结果。以下是一些您意想不到的结果的示例:
意外的真相
意外的结论
具有特殊功能的物体
You can actually write a JavaScript function that behaves exactly like
==
that should give you some insight into how it behaves.To show you what I mean here is that function:
As you can see
==
has a lot of complicated logic for type conversion. Because of that it's hard to predict what result you are going to get.Here are some examples of some results you wouldn't expect:
Unexpected Truths
Unexpected Conclusions
Objects with Special Functions