JSON.stringify(NaN)为什么等于null

JSON.stringify(NaN)
"null"
JSON.stringify([NaN])
"[null]"
JSON.stringify(undefined)
undefined
JSON.stringify(null)
"null"
JSON.stringify([undefined])
"[null]"

求解释原理


九州编程
浏览 1036回答 2
2回答

跃然一笑

从ecma-262 规范文档 的角度解释标题的问题就非常简单。直接看为什么 JSON.stringify(NaN) => "null"NOTE 4Finite numbers are stringified as if by calling ToString(number). NaN and Infinity regardless of sign are represented as the String null.NaN 会被 当成字符串 null为什么 JSON.stringify(undefined) => undefinedJSON.stringify([undefined]) => "[null]"NOTE 5Values that do not have a JSON representation (such as undefined and functions) do not produce a String. Instead they produce the undefined value. In arrays these values are represented as the String null

忽然笑

undefined 直接返回 undefined ; 在数组中,就被当成 字符串null;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript