admin管理员组文章数量:1424942
I've found that on Opera 11.50 the expression
JSON.stringify(2)
returns an object for which
typeof
returns"string"
constructor.name
isString
charCodeAt(0)
is 50length
is 1
But still
alert(JSON.stringify(2) == "2")
shows "false" in Opera (and the same happens using ===
).
Is this a bug or what?
The only way I found to make it pare equal to "2" is calling .substr(0)
(for example even adding an empty string still pares different).
I've found that on Opera 11.50 the expression
JSON.stringify(2)
returns an object for which
typeof
returns"string"
constructor.name
isString
charCodeAt(0)
is 50length
is 1
But still
alert(JSON.stringify(2) == "2")
shows "false" in Opera (and the same happens using ===
).
Is this a bug or what?
The only way I found to make it pare equal to "2" is calling .substr(0)
(for example even adding an empty string still pares different).
- 4 If it is so then it is a bug. Minimalistic sample to try would be helpful to say for sure. – c-smile Commented Aug 14, 2011 at 16:42
-
3
Interesting,
== 2
yieldstrue
... – Tomasz Nurkiewicz Commented Aug 14, 2011 at 16:44 - 4 Does it also return false if you use x === "2" ? – Tom Commented Aug 14, 2011 at 16:48
-
2
Another interesting thing is that the bug occurs with and only with numbers
0
to9
. – duri Commented Aug 14, 2011 at 17:05 - 2 How interesting, that I'm not suprised. Opera is crazy! – KARASZI István Commented Aug 14, 2011 at 17:06
1 Answer
Reset to default 7That definitely looks like a bug.
From the ECMAScript 5.1 specification:
Conforming implementations of JSON.parse and JSON.stringify must support the exact interchange format described in this specification without any deletions or extensions to the format. This differs from RFC 4627 which permits a JSON parser to accept non-JSON forms and extensions.
And:
JSON.stringify produces a String that conforms to the following JSON grammar. JSON.parse accepts a String that conforms to the JSON grammar
It may be that it somehow wraps the string in a "JSONText" type object which still has a typeof
of string
but that seems very odd.
I would definitely think that the following implementation in this case is the correct one:
JSON.stringify(2) == "2" && JSON.stringify(2) === "2" && JSON.stringify(2) == 2 && JSON.stringify(2) !== 2;
true
According to @6502 (see ment) 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/publications/files/ECMA-ST/Ecma-262.pdf
本文标签: javascriptIs it correct that JSONstringify(2)quot2quot may return falseStack Overflow
版权声明:本文标题:javascript - Is it correct that JSON.stringify(2) == "2" may return false? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745371002a2655729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论