gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:31:14 28 4
gpt4 key购买 nike

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





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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com