为什么 `throw` 在 ES6 箭头函数中无效?

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

我只是在寻找 为什么 这是无效的原因:

() => throw 42;

我知道我可以通过以下方式绕过它:

() => {throw 42};

如果您不使用块 ({}) 作为 arrow function, the body must be an expression 的主体:

ArrowFunction:
    ArrowParameters[no LineTerminator here] => ConciseBody

ConciseBody:
    [lookahead ≠ { ] AssignmentExpression
    { FunctionBody }

但是 throwstatement,不是表达式。


理论上

() => throw x;

等同于

() => { return throw x; }

这也无效。

你不能return throw这实际上是你想要做的:

function(){
  return throw 42;
}

如果在箭头函数中省略大括号,则会创建 an implicit return,这相当于使用大括号创建显式 return,如下所示:() => { return throw 42 };

但是,您只能returnexpressions, not statementsthrow 是一个声明。