为什么在 ES6 箭头函数中`throw` 无效? [英] Why is `throw` invalid in an ES6 arrow function?
本文介绍了为什么在 ES6 箭头函数中`throw` 无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想看看为什么这是无效的:
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};
推荐答案
如果您不使用块 ({}
) 作为 箭头函数,主体必须是表达式:
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 }
但是 throw
是一个 语句,不是表达式.
But throw
is a statement, not an expression.
理论上
() => throw x;
相当于
() => { return throw x; }
这也无效.
这篇关于为什么在 ES6 箭头函数中`throw` 无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文