【发布时间】:2013-06-20 13:06:35
【问题描述】:
虽然[] + [] 是一个空字符串,但[] + {} 是"[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)
【问题讨论】:
-
这只是浏览器控制台。尝试将 记录到 控制台,这与在 NodeJS 中的相同。 jsbin.com/oveyuj/1/edit
-
不是真正的重复,我要求 NodeJS 回答。投票重新开放...
-
嗯...对不起。但是,stackoverflow.com/questions/9032856/… 仍然相关并回答了前半部分
-
不要忘记
{}可以被解释为表达式或对象原语,具体取决于上下文。也许客户端和服务器上的代码相同,但由于输入代码的上下文不同,它对{}的解释不同。 -
请重新打开,然后一次又一次地停止关闭这个问题,因为这个问题真的不是重复的。
标签: javascript node.js eval google-chrome-devtools web-developer-toolbar