While[] + []是一个空字符串,[] + {}is"[object Object]"和{} + []is 0。为什么是{} + {}NaN?
> {} + {}
NaN
我的问题不是为什么({} + {}).toString()是"[object Object][object Object]"while NaN.toString(),这部分"NaN"在这里已经有了答案。
我的问题是为什么这只发生在客户端?在服务器端 ( 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)