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

标签 javascript ecmascript-6 arrow-functions

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

() => throw 42;

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

() => {throw 42};

最佳答案

如果您不使用 block ({}) 作为 arrow function 的主体,主体必须是 expression :

ArrowFunction:
    ArrowParameters[no LineTerminator here] => ConciseBody

ConciseBody:
    [lookahead ≠ { ] AssignmentExpression
    { FunctionBody }

但是抛出statement ,不是表达式。

<小时/>

理论上

() => throw x;

相当于

() => { return throw x; }

这也无效。

关于javascript - 为什么 `throw` 在 ES6 箭头函数中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109822/

相关文章:

javascript - 为什么我不能在 JavaScript/ES6 中使用带有箭头函数的 `new`?

javascript - Angular 2 fakeAsync 在使用 tick() 的函数中等待超时?

javascript - 关于在 JavaScript 中定义全局变量

node.js - 如果不存在,如何使用 Browserify 创建新目录?

javascript - 如何在箭头函数中使用 'this' 来引用被单击的元素

javascript - Reactjs app div onclick 在 IE 11 中不起作用,没有调用任何函数

javascript - CSS 分离头表技巧 : keep verticall scrollbar on screen and last cell go full width

javascript - 使 Javascript 线程变得更快

javascript - JS vue中如何将返回值放入另一个返回值中

javascript - 派生类究竟从其基类继承了什么?