[] + []
为空字符串,[] + {}
为"[object Object]"
,{} + []
为0
。为什么是{} + {}
NaN?
> {} + {}
NaN
我的问题不是为什么({} + {}).toString()
是"[object Object][object Object]"
,而NaN.toString()
是"NaN"
,this part has an answer here already。
我的问题是,为什么这种情况只发生在客户端?在服务器端(Node.js),{} + {}
是"[object Object][object Object]"
。
> {} + {}
'[object Object][object Object]'
总结
在客户端:
[] + [] // Returns ""
[] + {} // Returns "[object Object]"
{} + [] // Returns 0
{} + {} // Returns NaN
NaN.toString() // Returns "NaN"
({} + {}).toString() // Returns "[object Object][object Object]"
var a = {} + {}; // 'a' will be "[object Object][object Object]"
在Node.js中:
[] + [] // Returns "" (like on the client)
[] + {} // Returns "[object Object]" (like on the client)
{} + [] // Returns "[object Object]" (not like on the client)
{} + {} // Returns "[object Object][object Object]" (not like on the client)
https://stackoverflow.com/questions/17268468
复制相似问题