小编典典

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

node.js

While [] + []是一个空字符串,[] + {}is "[object Object]"{} + []is是0。为什么是{} + {}NaN?

> {} + {}
  NaN

我的问题是,为什么不({} + {}).toString()"[object Object][object Object]"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)

阅读 291

收藏
2020-07-07

共1个答案

小编典典

更新的注释:此问题已在Chrome
49中修复

非常有趣的问题!让我们深入。

根本原因

差异的根源在于Node.js如何评估这些语句与Chrome开发工具如何进行评估。

Node.js的作用

Node.js 为此使用了repl模块。

从Node.js
REPL源代码中

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工具执行以下操作

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使用表达式在对象上执行a 。表达式为:

with ((window && window.console && window.console._commandLineAPI) || {}) {
    {}+{};// <-- This is your code
}

因此,如您所见,表达式是直接求值的,而没有换行括号。

为什么Node.js的行为有所不同

Node.js的来源证明了这一点:

// This catches '{a : 1}' properly.

Node并不总是这样。这是改变它的实际提交。Ryan对该更改发表了以下评论:“改进了REPL命令的规避方式”,并举例说明了区别。


犀牛

更新-OP 对 Rhino的 行为方式(以及为何其行为像Chrome devtools和与nodejs不同的行为)感兴趣。

Rhino使用完全不同的JS引擎,这与Chrome开发人员工具和Node.js的REPL都使用V8不同。

这是在Rhino外壳中使用Rhino评估JavaScript命令时发生的事情的基本流程。

基本上:

Script script = cx.compileString(scriptText, "<command>", 1, null);
if (script != null) {
    script.exec(cx, getShellScope()); // <- just an eval
}

在这三者中,Rhino的外壳是最接近实物的外壳,eval没有任何包裹。Rhino的eval()陈述最接近实际陈述,您可以期望其表现完全相同eval

2020-07-07