javascript - 为什么 {} + {} 仅在客户端是 NaN?为什么不在 Node.js 中?

标签 javascript node.js eval google-chrome-devtools web-developer-toolbar

[] + [] 是一个空字符串,[] + {}"[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 命令时发生的基本管道。

基本上:

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/

相关文章:

javascript - 如何从 Firefox 扩展代码中获取本地 IP

node.js - 如何将生成的 pdf 保存在项目文件夹 Node Jasper Reports 中?

node.js - HTTPs 与用于 SPA 的expressjs staticFile

JavaScript/如何在 node.js 中创建一个新对象?

scheme - Racket :为什么不应用 "bitmap"功能?

python - 在字符串上使用 eval 会引发错误。

javascript - Animatable 重新渲染动画 React Native

javascript - 从表附加到另一个表时添加额外的列

javascript - 如何删除一个 “input type=file”倍数的具体值

function - Chrome - 评估 - 函数()