Why does calling a function in the Node.js REPL with )( work?

后端 未结 3 1645
你的背包
你的背包 2020-12-23 15:44

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

~$ node
> function hi() { console.log(\"Hello, World!\"); };
undefined
&         


        
3条回答
  •  感情败类
    2020-12-23 16:17

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

    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.

    Reproduced on v0.10.20.


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

提交回复
热议问题