使用 underscore.js 创建 javascript 自定义错误对象的快捷方式?

发布于 2024-12-15 13:58:15 字数 568 浏览 4 评论 0原文

有没有一种干净的方法以某种方式使用 underscore.js _.extend 函数(或任何其他函数)来创建从基 Error 类继承的自定义 Error 类?我正在寻找一种类似骨干的方式来做到这一点。

尝试过这个:

InternalError = function(message, args) {
    message || (message = {});
    this.initialize(message, args);
};
_.extend(InternalError.prototype, Error.prototype, {
    initialize: function(message, args) {
        this.message = message;
        this.name = 'InternalError';
    }
});

var error1 = new Error('foo');
var error2 = new InternalError('bar');
console.warn(error1, error2);
throw error2;

但它不起作用:(。

Is there a clean way to somehow use underscore.js _.extend function (or any other) to create custom Error classes that inherit from base Error class? I'm looking for a backbone-like way to do this.

Tried this :

InternalError = function(message, args) {
    message || (message = {});
    this.initialize(message, args);
};
_.extend(InternalError.prototype, Error.prototype, {
    initialize: function(message, args) {
        this.message = message;
        this.name = 'InternalError';
    }
});

var error1 = new Error('foo');
var error2 = new InternalError('bar');
console.warn(error1, error2);
throw error2;

But it is not working :(.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-12-22 13:58:15

(请原谅我关于原型继承的小括号。您可以跳过此部分并查看下面的答案。

为了让一个对象扩展另一个对象,的原型必须是您可以在网上找到许多关于此的好资源,但不幸的是,也有很多不好的资源,因此我建议您阅读这篇文章:http://javascript.crockford.com/prototypal.html
通过 new 关键字实例化的新对象new f() 返回其原型对象的副本:f.prototype.承认这一点,您意识到为了扩展对象 x,当前对象的原型必须是一个新的 x 实例:

function Person(){};
Person.prototype.speak = function(){
    alert("I'm a person");
}
function StackoverflowUser(){};
StackoverflowUser.prototype = new Person();
// now StackOverflowUser is a Person too

)

您实际上不需要 underscore.js那:

var InternalError = function(msg,args){
    return this.initialize(msg||{},args);
}

// inherit from the Error object
InternalError.prototype = new Error();

// overwrite the constructor prop too
InternalError.constructor = InternalError;
InternalError.prototype.initialize = function(msg,args){
    this.message = msg;
    this.name = 'InternalError';
}

var err = new InternalError("I'm an internal error!");
alert(err instanceof Error); // true
throw err;

如果你真的想使用 underscore.js :

var InternalError = function(msg,args){
    return this.initialize(msg||{},args);
}
_.extend(InternalError.prototype,new Error(),{
    initialize : function(msg,args){
        this.message = msg;
        this.name = 'InternalError';
    },
    constructor : InternalError
});

(Excuse my small parenthesis about prototypal inheritance. You can skip this and see the answer below.

In order for an object to extend another one, the child's prototype must be an instance of it's parent. You can find many good resources on the web about this, but, unfortunately, many bad ones as well, so I recommend you take a peak at this article : http://javascript.crockford.com/prototypal.html .
A new object instantiated via the new keyword : new f() returns a copy of it's prototype object : f.prototype. Acknowledging this, you realize that in order to extend an object x, your current object's prototype must be a new x instance :

function Person(){};
Person.prototype.speak = function(){
    alert("I'm a person");
}
function StackoverflowUser(){};
StackoverflowUser.prototype = new Person();
// now StackOverflowUser is a Person too

)

you don't actually need underscore.js for that :

var InternalError = function(msg,args){
    return this.initialize(msg||{},args);
}

// inherit from the Error object
InternalError.prototype = new Error();

// overwrite the constructor prop too
InternalError.constructor = InternalError;
InternalError.prototype.initialize = function(msg,args){
    this.message = msg;
    this.name = 'InternalError';
}

var err = new InternalError("I'm an internal error!");
alert(err instanceof Error); // true
throw err;

if you really want to use underscore.js :

var InternalError = function(msg,args){
    return this.initialize(msg||{},args);
}
_.extend(InternalError.prototype,new Error(),{
    initialize : function(msg,args){
        this.message = msg;
        this.name = 'InternalError';
    },
    constructor : InternalError
});
恏ㄋ傷疤忘ㄋ疼 2024-12-22 13:58:15

扩展错误对象(以及一般的主机对象)并不适用于所有浏览器。 Error.prototype 在 IE 上甚至不存在。不过,无需扩展 Error 对象,只需使用任何自定义对象即可,甚至是对象文字:{message: 'Really bad error}

Extending the error object (and host objects in general) does not work in all browsers. Error.prototype doesn't even exist on IE. There is no need to extend the Error object though, just using any custom object will do, even object literals: {message: 'Really bad error}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文