throw Error('msg')vs throw new Error('msg') [英] throw Error('msg') vs throw new Error('msg')
本文介绍了throw Error('msg')vs throw new Error('msg')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var err1 = Error('message');
var err2 = new Error('message');
有什么区别?在Chrome控制台上看,他们看起来是一样的。对象上的相同属性和相同的 __ proto __
链。几乎看起来像错误
像工厂一样。
What's the difference? Looking at them in the chrome console, they look identical. Same properties on the object and the same __proto__
chain. Almost seems like Error
acts like a factory.
哪一个是正确的,为什么?
Which one is correct and why?
推荐答案
两者都不错;这在规范中有明确规定:
Both are fine; this is explicitly stated in the specification:
...因此,函数调用
Error(...)
等效于对象创建表达式new Error ...)
具有相同的参数。
... Thus the function call
Error(…)
is equivalent to the object creation expressionnew Error(…)
with the same arguments.
这篇关于throw Error('msg')vs throw new Error('msg')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文