如何在 JavaScript 中创建自定义错误? [英] How do I create a custom Error in JavaScript?
问题描述
出于某种原因,构造函数委托在以下代码段中似乎不起作用:
For some reason it looks like constructor delegation doesn't work in the following snippet:
function NotImplementedError() {
Error.apply(this, arguments);
}
NotImplementedError.prototype = new Error();
var nie = new NotImplementedError("some message");
console.log("The message is: '"+nie.message+"'")
运行这个给出消息是:''
.关于原因的任何想法,或者是否有更好的方法来创建新的 Error
子类?apply
用于我不知道的本机 Error
构造函数是否有问题?
Running this gives The message is: ''
. Any ideas as to why, or if there is a better way to create a new Error
subclass? Is there a problem with apply
ing to the native Error
constructor that I don't know about?
推荐答案
更新你的代码,将你的原型分配给 Error.prototype,instanceof 和你的断言工作.
Update your code to assign your prototype to the Error.prototype and the instanceof and your asserts work.
function NotImplementedError(message = "") {
this.name = "NotImplementedError";
this.message = message;
}
NotImplementedError.prototype = Error.prototype;
但是,我只会抛出您自己的对象并检查 name 属性.
However, I would just throw your own object and just check the name property.
throw {name : "NotImplementedError", message : "too lazy to implement"};
根据评论进行编辑
在查看评论并试图记住为什么我会将原型分配给 Error.prototype
而不是 new Error()
就像 Nicholas Zakas 在他的 文章,我使用以下代码创建了一个 jsFiddle:
After looking at the comments and trying to remember why I would assign prototype to Error.prototype
instead of new Error()
like Nicholas Zakas did in his article, I created a jsFiddle with the code below:
function NotImplementedError(message = "") {
this.name = "NotImplementedError";
this.message = message;
}
NotImplementedError.prototype = Error.prototype;
function NotImplementedError2(message = "") {
this.message = message;
}
NotImplementedError2.prototype = new Error();
try {
var e = new NotImplementedError("NotImplementedError message");
throw e;
} catch (ex1) {
console.log(ex1.stack);
console.log("ex1 instanceof NotImplementedError = " + (ex1 instanceof NotImplementedError));
console.log("ex1 instanceof Error = " + (ex1 instanceof Error));
console.log("ex1.name = " + ex1.name);
console.log("ex1.message = " + ex1.message);
}
try {
var e = new NotImplementedError2("NotImplementedError2 message");
throw e;
} catch (ex1) {
console.log(ex1.stack);
console.log("ex1 instanceof NotImplementedError2 = " + (ex1 instanceof NotImplementedError2));
console.log("ex1 instanceof Error = " + (ex1 instanceof Error));
console.log("ex1.name = " + ex1.name);
console.log("ex1.message = " + ex1.message);
}
控制台输出是这样的.
undefined
ex1 instanceof NotImplementedError = true
ex1 instanceof Error = true
ex1.name = NotImplementedError
ex1.message = NotImplementedError message
Error
at window.onload (http://fiddle.jshell.net/MwMEJ/show/:29:34)
ex1 instanceof NotImplementedError2 = true
ex1 instanceof Error = true
ex1.name = Error
ex1.message = NotImplementedError2 message
这证实了问题"我遇到的是错误的堆栈属性是创建 new Error()
的行号,而不是 throw e
发生的地方.但是,这可能比具有影响 Error 对象的 NotImplementedError.prototype.name = "NotImplementedError"
行的副作用更好.
This confirmes the "problem" I ran into was the stack property of the error was the line number where new Error()
was created, and not where the throw e
occurred. However, that may be better that having the side effect of a NotImplementedError.prototype.name = "NotImplementedError"
line affecting the Error object.
另外,注意NotImplementedError2
,当我没有明确设置.name
时,它等于Error".但是,正如评论中提到的,因为该版本将原型设置为 new Error()
,所以我可以设置 NotImplementedError2.prototype.name = "NotImplementedError2"
并且没问题.
Also, notice with NotImplementedError2
, when I don't set the .name
explicitly, it is equal to "Error". However, as mentioned in the comments, because that version sets prototype to new Error()
, I could set NotImplementedError2.prototype.name = "NotImplementedError2"
and be OK.
这篇关于如何在 JavaScript 中创建自定义错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!