这个问题在这里已经有了答案:
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 expressionnew Error(…)
with the same arguments.
关于javascript - `throw new Error` 和 `throw Error` 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61339917/