Why do we need to collapse the cast using brackets in es6 arrow functions

I want to ask if anyone knows the reason why

const fun = msg => throw new Error( msg ); invalid js while

const fun = msg => { throw new Error( msg ); } really

WITH

const fun = msg => new Error( t ); really

Someone (I would) would expect the case to throwbe valid JS.

Is there any reason for this choice? for example, does this make it difficult for a translator or something like that?

+4
source share
1 answer

The arrow functions are defined as follows:

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

And throw is a statement:

ThrowStatement[Yield] :
    throw [no LineTerminator here] Expression[In, ?Yield] ;

Then the first syntax of the ConciseBody element will not work, because the AssignmentExpression does not contain statements:

AssignmentExpression :
    YieldExpression
    ArrowFunction
    LeftHandSideExpression = AssignmentExpression
    LeftHandSideExpression AssignmentOperator AssignmentExpression
    BitwiseANDExpression : BitwiseANDExpression & EqualityExpression
    BitwiseXORExpression : BitwiseXORExpression ^ BitwiseANDExpression
    BitwiseORExpression : BitwiseORExpression | BitwiseXORExpression

, ConciseBody FunctionBody, . ThrowStatement.

+8

All Articles