Mar*_*ahn 27 javascript ecmascript-6 arrow-functions
我只是想找出为什么这是无效的原因:
() => throw 42;
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过以下方式绕过它:
() => {throw 42};
Run Code Online (Sandbox Code Playgroud)
Fel*_*ing 31
如果不使用block({})作为箭头函数的主体,则主体必须是表达式:
ArrowFunction:
ArrowParameters[no LineTerminator here] => ConciseBody
ConciseBody:
[lookahead ? { ] AssignmentExpression
{ FunctionBody }
Run Code Online (Sandbox Code Playgroud)
但这throw是一种陈述,而不是表达.
理论上
() => throw x;
Run Code Online (Sandbox Code Playgroud)
相当于
() => { return throw x; }
Run Code Online (Sandbox Code Playgroud)
这也是无效的.
你不能return throw这实际上是你想要做的事情:
function(){
return throw 42;
}
Run Code Online (Sandbox Code Playgroud)