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

后端 未结 3 1625
你的背包
你的背包 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:04

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

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

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

提交回复
热议问题