ES6 immediately invoked arrow function

后端 未结 4 1448
梦谈多话
梦谈多话 2020-11-28 02:16

Why does this work in a Node.js console (tested in 4.1.1 and 5.3.0) but doesn\'t work in the browser (tested in Chrome)? This code block should create and invok

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 02:49

    None of these should work without parentheses.

    Why?

    Because according in the spec:

    1. ArrowFunction is listed under AssignmentExpression
    2. The LHS of a CallExpression must be a MemberExpression, SuperCall or CallExpression

    So an ArrowFunction cannot be on the LHS of a CallExpression.


    What this effectively means in how => should be interpreted, is that it works on the same sort of level as assignment operators =, += etc.

    Meaning

    • x => {foo}() doesn't become (x => {foo})()
    • The interpreter tries to interpret it as x => ({foo}())
    • Thus it's still a SyntaxError
    • So the interpreter decides that the ( must have been wrong and throws a SyntaxError

    There was a bug on Babel about it here, too.

提交回复
热议问题