JSON.stringify(2) == "2" 是否正确?可能返回 false?
我发现在 Opera 11.50 上,表达式
JSON.stringify(2)
返回一个对象,其中
typeof
返回"string"
-
constructor.name
是String< /code>
-
charCodeAt(0)
is 50 -
length
is 1
但
alert(JSON.stringify(2) == "2")
在 Opera 中仍然显示“false”(使用 ===
也会发生同样的情况)。
这是一个错误还是什么?
我发现使其比较等于“2”的唯一方法是调用 .substr(0)
(例如,即使添加空字符串仍然比较不同)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对看起来像一个错误。
来自 ECMAScript 5.1 规范:
和:
它可能以某种方式将字符串包装在“JSONText”类型对象中,该对象仍然具有
typeof
为string
但这看起来很奇怪。我肯定认为在这种情况下以下实现是正确的:
根据@6502(参见评论),这是
true
in:铬合金;火狐浏览器; IE9; iPad Safari; OSX Safari; N1 Android 浏览器
ECMAScript 5.1 规范文档: http:// /www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
That definitely looks like a bug.
From the ECMAScript 5.1 specification:
And:
It may be that it somehow wraps the string in a "JSONText" type object which still has a
typeof
ofstring
but that seems very odd.I would definitely think that the following implementation in this case is the correct one:
According to @6502 (see comment) this is
true
in:Chrome; Firefox; IE9; iPad Safari; OsX Safari; the N1 Android browser
The ECMAScript 5.1 specification document: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf