为什么在节点中调用函数。与)(工作?

时间:2022-06-05 00:37:11

Why is it possible to call function in JavaScript like this, tested with node.js:

为什么在JavaScript中调用函数就像这样,用node.js测试?

~$ node
> function hi() { console.log("Hello, World!"); };
undefined
> hi
[Function: hi]
> hi()
Hello, World!
undefined
> hi)( // WTF?
Hello, World!
undefined
>

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?

为什么最后一个电话是“你好”?是节点中的bug吗?js, V8引擎中的bug,官方未定义的行为,还是所有解释器的有效JavaScript ?

3 个解决方案

#1


83  

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

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

function hi() { console.log("Hello, World!"); }
hi)(

Error:

错误:

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

Issue submitted #6634.

问题提交# 6634。

Reproduced on v0.10.20.

v0.10.20复制。


v0.11.7 have this fixed.

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

#2


201  

It's due to how the REPL evaluates the input, which is ultimately as:

这是由于REPL如何评估输入,最终为:

(hi)()

The additional parenthesis are added to force it to be an Expression:

附加的括号被添加到强制它为一个表达式:

  // First we attempt to eval as expression with parens.
  // This catches '{a : 1}' properly.
  self.eval('(' + evalCmd + ')',
      // ...

The intent is to treat {...} as Object literals/initialisers rather than as a block.

其目的是治疗{…}作为对象常量/初始化符,而不是块。

var stmt = '{ "foo": "bar" }';
var expr = '(' + stmt + ')';

console.log(eval(expr)); // Object {foo: "bar"}
console.log(eval(stmt)); // SyntaxError: Unexpected token :

And, as leesei mentioned, this has been changed for 0.11.x, which will just wrap { ... } rather than all input:

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

#3


60  

There was a bug raised 4 months back, for this issue https://github.com/joyent/node/issues/5698

4个月前有一个bug被发现,这个问题是https://github.com/joyent/node/issues/5698

And the problem was because, REPL encloses the statements with parens. So

问题是,REPL将语句括起来。所以

foo)(

becomes

就变成了

(foo)()

Actual explanation can be found here https://github.com/joyent/node/issues/5698#issuecomment-19487718.

实际的解释可以在这里找到https://github.com/joyent/node/issues/5698#issue -19487718。

#1


83  

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

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

function hi() { console.log("Hello, World!"); }
hi)(

Error:

错误:

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

Issue submitted #6634.

问题提交# 6634。

Reproduced on v0.10.20.

v0.10.20复制。


v0.11.7 have this fixed.

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

#2


201  

It's due to how the REPL evaluates the input, which is ultimately as:

这是由于REPL如何评估输入,最终为:

(hi)()

The additional parenthesis are added to force it to be an Expression:

附加的括号被添加到强制它为一个表达式:

  // First we attempt to eval as expression with parens.
  // This catches '{a : 1}' properly.
  self.eval('(' + evalCmd + ')',
      // ...

The intent is to treat {...} as Object literals/initialisers rather than as a block.

其目的是治疗{…}作为对象常量/初始化符,而不是块。

var stmt = '{ "foo": "bar" }';
var expr = '(' + stmt + ')';

console.log(eval(expr)); // Object {foo: "bar"}
console.log(eval(stmt)); // SyntaxError: Unexpected token :

And, as leesei mentioned, this has been changed for 0.11.x, which will just wrap { ... } rather than all input:

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

#3


60  

There was a bug raised 4 months back, for this issue https://github.com/joyent/node/issues/5698

4个月前有一个bug被发现,这个问题是https://github.com/joyent/node/issues/5698

And the problem was because, REPL encloses the statements with parens. So

问题是,REPL将语句括起来。所以

foo)(

becomes

就变成了

(foo)()

Actual explanation can be found here https://github.com/joyent/node/issues/5698#issuecomment-19487718.

实际的解释可以在这里找到https://github.com/joyent/node/issues/5698#issue -19487718。