Javascript 为什么在Node.js REPL中调用一个函数会起作用?

Javascript 为什么在Node.js REPL中调用一个函数会起作用?,javascript,node.js,syntax,Javascript,Node.js,Syntax,为什么可以像这样在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中的bug,V

为什么可以像这样在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中的bug,V8引擎中的bug,官方未定义的行为,还是所有解释器的JavaScript都有效?

似乎是node REPL bug,将这两行放在
.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
提交的问题

复制于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)
> 
$nvm运行0.11.7
运行节点v0.11.7
>函数hi(){console.log(“你好,世界!”);}
未定义
>嗨)(
SyntaxError:意外标记)
在Object.exports.createScript(vm.js:44:10)
在REPLServer.defaultEval(repl.js:117:23)
在REPLServer.b[as eval](domain.js:251:18)
在接口处。(回复js:277:12)
位于Interface.EventEmitter.emit(events.js:103:17)
在线(readline.js:194:10)
在接口处。\u行(readline.js:523:8)
在接口处写入(readline.js:798:14)
在ReadStream.onkeypress(readline.js:98:10)
在ReadStream.EventEmitter.emit(events.js:106:17)
> 

这取决于REPL如何评估输入,最终是:

(hi)()
添加了附加括号:

其目的是将
{…}
视为
对象
文本/而不是一个文本

并且,正如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';
  }

4个月前,针对此问题提出了一个错误

问题是,REPL用paren封装语句。所以

foo)(
变成

(foo)()

具体的解释可以在这里找到。

可在Ubuntu 13.04上的nodejs v0.6.19中复制在jsfiddle.net上的一个快速测试将向您显示它是无效的JavaScript。似乎是一个Node REPL错误,将这两行放在
中。js
将导致语法错误BTW,这在irc(FreeNode#nodejs)上出现,by@minimlPerl也有类似的功能,原因大致相同:
perl-ne'$x+=$\u;}{print$x'
。看到了,我认为自动插入分号不好。这是否意味着
hi)(arg
会起作用?这可能会被滥用来编写一些真正充斥着WTF的代码;-)我仍然不明白为什么会运行它。它不会因为不匹配的open paren而导致语法错误?
hi)(arg
变成
(hi)(arg)
--没有什么不匹配的东西他们真的开始修复了吗?啊,遗憾的是,我真的很想看到它开始一种文化并成为所有语言的一种功能。我打了多少次)(而不是()匆忙…:)@geomagas你认为
函数a)arg1,arg2(})arg2+arg1[return;{
应该是有效的语法吗?不,不完全是。实际上,这是一个笑话。很久以前,有一个Lisp实现,带有DWIM选项,可以自动更正拼写错误和其他小错误。@geomagas,好吧,有些人已经开始考虑了-
npm
install
isntall
.bet你没有注意到:)
foo)(
(foo)()