为什么在 Node.js REPL 中使用 )( 调用函数会起作用
Why does calling a function in the Node.js REPL with )( work?
为什么可以在JavaScript中调用这样的函数,用node测试.js:
~$ node
> function hi() { console.log("Hello, World!"); };
undefined
> hi
[Function: hi]
> hi()
Hello, World!
undefined
> hi)( // WTF?
Hello, World!
undefined
>
为什么最后一个调用 hi)(
有效?是node.js中的错误,V8引擎中的错误,官方未定义的行为,还是对所有解释器实际上有效的JavaScript?
这是由于 REPL 如何评估输入,最终为:
(hi)()
添加额外的括号以强制其为表达式:
// First we attempt to eval as expression with parens.
// This catches '{a : 1}' properly.
self.eval('(' + evalCmd + ')',
// ...
目的是将{...}
视为Object
文字/初始化器,而不是块。
var stmt = '{ "foo": "bar" }';
var expr = '(' + stmt + ')';
console.log(eval(expr)); // Object {foo: "bar"}
console.log(eval(stmt)); // SyntaxError: Unexpected token :
而且,正如leesei所提到的,这已经针对0.11.x进行了更改,它将仅包装{ ... }
而不是所有输入:
if (/^'s*'{/.test(evalCmd) && /'}'s*$/.test(evalCmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
evalCmd = '(' + evalCmd + ')'n';
} else {
// otherwise we just append a 'n so that it will be either
// terminated, or continued onto the next expression if it's an
// unexpected end of input.
evalCmd = evalCmd + ''n';
}
似乎是一个 Node REPL 错误,将这两行放在一个.js
会导致语法错误。
function hi() { console.log("Hello, World!"); }
hi)(
错误:
SyntaxError: Unexpected token )
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
问题已提交 #6634。
转载于 v0.10.20。
v0.11.7 已修复此问题。
$ nvm run 0.11.7
Running node v0.11.7
> function hi() { console.log("Hello, World!"); }
undefined
> hi)(
SyntaxError: Unexpected token )
at Object.exports.createScript (vm.js:44:10)
at REPLServer.defaultEval (repl.js:117:23)
at REPLServer.b [as eval] (domain.js:251:18)
at Interface.<anonymous> (repl.js:277:12)
at Interface.EventEmitter.emit (events.js:103:17)
at Interface._onLine (readline.js:194:10)
at Interface._line (readline.js:523:8)
at Interface._ttyWrite (readline.js:798:14)
at ReadStream.onkeypress (readline.js:98:10)
at ReadStream.EventEmitter.emit (events.js:106:17)
>
4 个月前有一个错误,针对此问题 https://github.com/joyent/node/issues/5698
问题是,REPL用参数附上了语句。所以
foo)(
成为
(foo)()
实际解释可以在这里找到 https://github.com/joyent/node/issues/5698#issuecomment-19487718。
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量