为什么在Node.js REPL中调用函数()工作? [英] Why does calling a function in the Node.js REPL with )( work?

查看:85
本文介绍了为什么在Node.js REPL中调用函数()工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么可以在这样的JavaScript中调用函数,使用node.js进行测试:

Why is it possible to call function in JavaScript like this, tested with 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中的bug,V8引擎中的bug,官方未定义的行为,还是实际有效所有解释器的JavaScript?

Why does the last call, hi)(, work? Is it bug in node.js, bug in V8 engine, officially undefined behaviour, or actually valid JavaScript for all interpreters?

推荐答案

似乎是一个Node REPL错误,将这两行放在中.js 会导致语法错误。

Seems to be a Node REPL bug, putting these two lines in a .js will cause syntax error.

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。

Reproduced on v0.10.20.

v0.11.7已修复此问题。

v0.11.7 have this fixed.

$ 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)
> 

这篇关于为什么在Node.js REPL中调用函数()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆