抛出错误('msg')与抛出新错误('msg') [英] throw Error('msg') vs throw new Error('msg')

查看:42
本文介绍了抛出错误('msg')与抛出新错误('msg')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var err1 = Error('message');
var err2 = new Error('message');

有什么区别?在 chrome 控制台中查看它们,它们看起来相同.对象上的相同属性和相同的 __proto__ 链.几乎看起来 Error 就像一个工厂.

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 expression new Error(…) with the same arguments.

这篇关于抛出错误('msg')与抛出新错误('msg')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆