Programing

Node.js REPL에서) (을 사용하여 함수를 호출하는 이유는 무엇입니까?

lottogame 2020. 5. 12. 07:56
반응형

Node.js REPL에서) (을 사용하여 함수를 호출하는 이유는 무엇입니까?


node.js로 테스트 한 JavaScript에서 함수를 호출 할 수있는 이유는 무엇입니까?

~$ 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입니까?


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

REPL이 입력을 평가하는 방식 때문입니다. 결과는 다음과 같습니다.

(hi)()

추가 괄호가 추가 되어 식이 되도록합니다 .

  // 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:

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

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

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

foo)(

becomes

(foo)()

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

참고URL : https://stackoverflow.com/questions/19310788/why-does-calling-a-function-in-the-node-js-repl-with-work

반응형