为什么在ES6箭头函数中`throw`无效? [英] Why is `throw` invalid in an ES6 arrow function?
本文介绍了为什么在ES6箭头函数中`throw`无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
)=>投掷42
我知道我可以通过以下方式解决:
()=> {throw 42};
解决方案
如果不使用块( {}
)作为箭头功能,身体必须是 表达式 :
ArrowFunction:
ArrowParameters [no LineTerminator here] => ConciseBody
ConciseBody:
[lookahead≠{] AssignmentExpression
{FunctionBody}
但是 throw
是一个 语句 ,而不是表达式。
理论上
()=>扔x
相当于
()=> {return throw x;
这也不是有效的。
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 }
But throw
is a statement, not an expression.
In theory
() => throw x;
is equivalent to
() => { return throw x; }
which would not be valid either.
这篇关于为什么在ES6箭头函数中`throw`无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文