继承自Error对象 - message属性在哪里? [英] Inheriting from the Error object - where is the message property?

查看:329
本文介绍了继承自Error对象 - message属性在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript中定义自定义错误对象时发现了一个奇怪的行为:

I noticed a strange behavior while defining custom error objects in Javascript:

function MyError(msg) {
    Error.call(this, msg);
    this.name = "MyError";
}
MyError.prototype.__proto__ = Error.prototype;

var error = new Error("message");
error.message; // "message"

var myError = new MyError("message");
myError instanceof Error; // true
myError.message; // "" !

为什么新错误(消息)设置消息属性,而 Error.call(this,msg); 不是?当然,我可以在 MyError 构造函数中定义 this.message = msg ,但我不太明白为什么它尚未设置在第一位。

Why does new Error("message") set the message property, while Error.call(this, msg); does not? Sure, I can just define this.message = msg in the MyError constructor, but I don't quite understand why it is not already set in the first place.

推荐答案

A。就像Raynos说的那样,没有设置消息的原因是错误是一个返回新错误的函数对象并且以任何方式操纵

A. Like, Raynos said, The reason message isn't being set is that Error is a function that returns a new Error object and does not manipulate this in any way.

B.这样做的方法是在 this 上设置构造函数的apply的结果,并以通常复杂的javascripty方式设置原型:

B. The way to do this right is to set the result of the apply from the constructor on this, as well as setting the prototype in the usual complicated javascripty way:

function MyError() {
    var tmp = Error.apply(this, arguments)
    tmp.name = this.name = 'MyError'

    this.message = tmp.message
    // instead of this.stack = ..., a getter for more optimizy goodness
    Object.defineProperty(this, 'stack', {
        get: function () {
            return tmp.stack
        }
    })

    return this
}
var IntermediateInheritor = function () {}
IntermediateInheritor.prototype = Error.prototype
MyError.prototype = new IntermediateInheritor()

var myError = new MyError("message")
console.log("The message is: '"+myError.message+"'") // The message is: 'message'
console.log(myError instanceof Error)                    // true
console.log(myError instanceof MyError)                  // true
console.log(myError.toString())                          // MyError: message
console.log(myError.stack)                               // MyError: message \n 
                                                          // <stack trace ...>

此时这种方式的唯一问题(我已经把它弄了一下)那是

The only problems with this way of doing it at this point (i've iteratted it a bit) are that


  • stack 消息之外的属性不包含在 MyError 中,而

  • 堆栈跟踪还有一行不是必需的。

  • properties other than stack and message aren't included in MyError, and
  • the stacktrace has an additional line that isn't really necessary.

第一个问题可以通过使用此答案中的技巧迭代错误的所有非可枚举属性来修复:是否可以获得非-enumerable一个对象的继承属性名称?,但是< 9不支持这个。第二个问题可以通过撕掉堆栈跟踪中的那条线来解决,但我不确定如何安全地执行此操作(可能只是删除了第二行e.stack.toString()??)。

The first problem could be fixed by iterating through all the non-enumerable properties of error using the trick in this answer: Is it possible to get the non-enumerable inherited property names of an object?, but this isn't supported by ie<9. The second problem could be solved by tearing out that line in the stack trace, but I'm not sure how to safely do that (maybe just removing the second line of e.stack.toString() ??).

更新

我创建了一个继承库来执行此操作^ https://github.com/fresheneesz/proto

I created an inheritance library that does this ^ https://github.com/fresheneesz/proto

这篇关于继承自Error对象 - message属性在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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