而 [] + []
是一个空字符串,[] + {}
是 "[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)
最佳答案
更新注释:this has been fixed in Chrome 49 。
非常有趣的问题!让我们深入研究。
根本原因
区别的根源在于 Node.js 评估这些语句的方式与 Chrome 开发工具的方式。
Node.js 的作用
Node.js 为此使用 repl 模块。
来自 Node.js REPL source code:
self.eval(
'(' + evalCmd + ')',
self.context,
'repl',
function (e, ret) {
if (e && !isSyntaxError(e))
return finish(e);
if (typeof ret === 'function' && /^[\r\n\s]*function/.test(evalCmd) || e) {
// Now as statement without parens.
self.eval(evalCmd, self.context, 'repl', finish);
}
else {
finish(null, ret);
}
}
);
这就像在 Chrome 开发者工具中运行 ({}+{})
一样,它也会像你一样生成 "[object Object][object Object]"
会期待的。
chrome 开发者工具的作用
另一方面 Chrome dveloper tools does the following :
try {
if (injectCommandLineAPI && inspectedWindow.console) {
inspectedWindow.console._commandLineAPI = new CommandLineAPI(this._commandLineAPIImpl, isEvalOnCallFrame ? object : null);
expression = "with ((window && window.console && window.console._commandLineAPI) || {}) {\n" + expression + "\n}";
}
var result = evalFunction.call(object, expression);
if (objectGroup === "console")
this._lastResult = result;
return result;
}
finally {
if (injectCommandLineAPI && inspectedWindow.console)
delete inspectedWindow.console._commandLineAPI;
}
所以基本上,它使用表达式对对象执行 call
。表达式为:
with ((window && window.console && window.console._commandLineAPI) || {}) {
{}+{};// <-- This is your code
}
因此,如您所见,表达式是直接计算的,没有括号。
为什么 Node.js 的行为不同
Node.js 的源代码证明了这一点:
// This catches '{a : 1}' properly.
Node 并不总是这样。这是 the actual commit that changed it 。 Ryan 对更改留下了以下评论:“改进 REPL 命令的评估方式”,并举例说明了不同之处。
犀牛
更新 - OP 对 Rhino 的行为方式很感兴趣(以及为什么它的行为类似于 Chrome devtools 而不同于 nodejs)。
Rhino 使用完全不同的 JS 引擎,这与 Chrome 开发工具和 Node.js 的 REPL 都使用 V8 不同。
这是在 Rhino shell 中使用 Rhino 评估 JavaScript 命令时发生的基本管道。
shell 运行
org.mozilla.javascript.tools.shell.main
。反过来,它会调用 this
new IProxy(IProxy.EVAL_INLINE_SCRIPT);
,例如,如果代码是直接使用内联开关 -e 传递的。这命中了 IProxy 的
run
方法。它调用
evalInlineScript
( src )。这只是编译字符串并对其进行评估。
基本上:
Script script = cx.compileString(scriptText, "<command>", 1, null);
if (script != null) {
script.exec(cx, getShellScope()); // <- just an eval
}
在这三个中,Rhino 的 shell 是最接近实际 eval
的一个,没有任何包装。 Rhino 最接近实际的 eval()
语句,您可以期望它的行为与 eval
完全一样。
关于javascript - 为什么 {} + {} 仅在客户端是 NaN?为什么不在 Node.js 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268468/