[英]How JavaScript's (0 == “”) === true agrees with ECMA-262 type conversion rules?
I've already seen several questions explaining why 0 == ""
is true
in JavaScript , but i have a bit deeper question. 我已经看过几个问题来解释为什么0 == ""
在JavaScript中为true
,但是我还有一个更深层的问题。
So the answer to why is 0 == ""
is true
in JavaScript is that string ""
gets converted to number, zero-length string is converted to zero number, but how this agrees with [9.3.1 paragraph of ECMA-262](http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1)
which says that string should be parsed using given formal grammar and if it fails, then such string is converted to NaN
. 因此,为什么在JavaScript中0 == ""
为true
的答案是字符串""
转换为数字,零长度字符串转换为零数字,但这与[9.3.1 paragraph of ECMA-262](http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1)
如何一致[9.3.1 paragraph of ECMA-262](http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1)
,该字符串应使用给定的形式语法进行解析,如果失败,则将其转换为NaN
。 When i looked on this spec i thought that ""
is not a string numeric literal and it should be parsed as NaN
and NaN
is not equal to 0.0
. 当我查看此规范时,我认为""
不是字符串数字文字,应将其解析为NaN
而NaN
不等于0.0
。
So why above speculation isn't correct and 0 == ""
is actually true
? 那么,为什么上述猜测是不正确的,而0 == ""
实际上是true
呢?
Thanks in advance. 提前致谢。
The grammar allows for StringNumericLiteral to be empty: 语法允许StringNumericLiteral为空:
StringNumericLiteral ::: StringNumericLiteral :::
StrWhiteSpace opt StrWhiteSpace 选择
StrWhiteSpace opt StrNumericLiteral StrWhiteSpace opt StrWhiteSpace 选择 StrNumericLiteral StrWhiteSpace 选择
A few lines down, it says: 几行下来,它说:
A StringNumericLiteral that is empty or contains only white space is converted to +0 . 空或仅包含空格的StringNumericLiteral会转换为+0 。
and: 和:
The MV of StringNumericLiteral ::: [empty] is 0. StringNumericLiteral ::: [空]的MV为0。
So I'm afraid you simply didn't fully read the standard passage you're looking at. 因此,恐怕您只是没有完全阅读要查看的标准文章。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.