Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

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

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

() => throw 42;

I know I can get around it via:

() => {throw 42};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...