javascript - `throw new Error` 和 `throw Error` 之间的区别?

标签 javascript

这个问题在这里已经有了答案:





throw Error('msg') vs throw new Error('msg')

(2 个回答)


24 天前关闭。




没有 new 时抛出错误有什么缺点吗?关键词?

throw new Error('Something went wrong');

/* vs */

throw Error('Something went wrong');

最佳答案

它们完全一样,保证 by the specification :

19.5.1 The Error Constructor

The Error constructor:

...

creates and initializes a new Error object when called as a function rather than as a constructor. Thus the function call Error(…) is equivalent to the object creation expression new Error(…) with the same arguments.

关于javascript - `throw new Error` 和 `throw Error` 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61339917/

相关文章:

javascript - Javascript 事件处理程序使用其范围之外的变量时出现问题

javascript - 在android中使用javascript设置内容时的编码问题

javascript - 无法从 Windows 10 Cordova 应用程序中的 Web View 进行 ajax 调用

javascript - 如何在 svelte 中创建响应式类

javascript - Google reCaptcha 未显示在 Angular 2 应用程序中

javascript - 循环内 Lodash Throttle 或 Debounce 的问题

javascript - 我正在尝试使用 jquery ajax 淡入和淡出 css 背景颜色?

javascript - HTML 4 等同于 HTML 5 的 FileReader?

javascript - 从函数返回的数组解构内的大括号

javascript - Ant design - 有没有办法获得我实际使用的样式?