Why is `throw` invalid in an ES6 arrow function?

后端 未结 3 1529
天涯浪人
天涯浪人 2020-12-01 21:00

I\'m just looking for a reason as to why this is invalid:

() => throw 42;

I know I can get around it via:

() =&         


        
3条回答
  •  暖寄归人
    2020-12-01 21:17

    If you don't use a block ({}) as body of an arrow function, the body must be an expression:

    ArrowFunction:
        ArrowParameters[no LineTerminator here] => ConciseBody
    
    ConciseBody:
        [lookahead ≠ { ] AssignmentExpression
        { FunctionBody }
    

    But throw is a statement, not an expression.


    In theory

    () => throw x;
    

    is equivalent to

    () => { return throw x; }
    

    which would not be valid either.

提交回复
热议问题