出于某种原因,在下面的代码段中,构造函数委托似乎不起作用:

function NotImplementedError() { 
  Error.apply(this, arguments); 
}
NotImplementedError.prototype = new Error();

var nie = new NotImplementedError("some message");
console.log("The message is: '"+nie.message+"'")

运行该命令得到的消息是:"。有什么想法,为什么,或者是否有更好的方法来创建一个新的错误子类?是否有一个问题,应用到本机错误构造函数,我不知道?


当前回答

以上所有的答案都很糟糕,真的。即使是107个向上的那个!真正的答案在这里:

从Error对象继承- message属性在哪里?

TL; diana:

a .没有设置message的原因是Error是一个函数,它返回一个新的Error对象,并且不以任何方式操作它。

B.正确的方法是从构造函数返回apply的结果,以及以通常复杂的javascript方式设置原型:

function MyError() { var temp = Error.apply(this, arguments); temp.name = this.name = 'MyError'; this.message = temp.message; if(Object.defineProperty) { // getter for more optimizy goodness /*this.stack = */Object.defineProperty(this, 'stack', { get: function() { return temp.stack }, configurable: true // so you can change it if you want }) } else { this.stack = temp.stack } } //inherit prototype using ECMAScript 5 (IE 9+) MyError.prototype = Object.create(Error.prototype, { constructor: { value: MyError, writable: true, configurable: true } }); 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 ...> //for EMCAScript 4 or ealier (IE 8 or ealier), inherit prototype this way instead of above code: /* var IntermediateInheritor = function() {}; IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor(); */

您可能会使用一些技巧来枚举tmp Error的所有不可枚举属性来设置它们,而不是显式地只设置stack和message,但是ie<9不支持这种技巧

其他回答

这在Cesium DeveloperError中很好地实现了:

文档 源

它的简化形式是:

var NotImplementedError = function(message) {
    this.name = 'NotImplementedError';
    this.message = message;
    this.stack = (new Error()).stack;
}

// Later on...

throw new NotImplementedError();

上面的很多方法都不起作用。

最后一个是一个实际的错误。如果您使用字符串,它看起来很好,但它不会提供堆栈跟踪。如果你抛出错误,你不能有“Uncaught BadError: bad”,所以你必须删除自定义错误(遗憾的是)。如果你抛出一个对象,它看起来有点偏离,最后一个只是一个平均错误。

此方法创建一个具有自定义名称的错误,同时保留堆栈跟踪:

var errProto = Object.create(Error.prototype, { constructor: { value: Error, enumerable: false, writable: true, configurable: true } }) var isFirefox = !!window.InstallTrigger // Hide stack for Firefox only, as stacks can cause problems with high "frame" counts. function createError(name, message, hideStack) { if (message == null) { message = "" } var customError = Error(message) customError.name = name Object.setPrototypeOf(customError, errProto) if (isFirefox && hideStack) { customError.stack = "" } else if (isFirefox) { var stack = customError.stack var newline = stack.indexOf("\n") + 1 stack = stack.slice(newline) customError.stack = stack var split = stack.split(":") if (split.length > 4) { var a = split[3] var b = split[4] var t = b.slice(0, b.indexOf("\n")) customError.lineNumber = Number(a) customError.columnNumber = Number(t) } } else { var stack = customError.stack var split = stack.split("\n") var secondPart = split.slice(2).join("\n") stack = split[0] + "\n" + secondPart customError.stack = stack var split = secondPart.split(":") var a = split[2] var b = split[3] } throw customError } var frame = 0 function aFunction() { if (++frame === 100) { createError("LazyError", "function getting lazy", false, true) } else { requestAnimationFrame(aFunction) } } setTimeout(aFunction, Math.random() * 500) * { font-family: Verdana; } Check your inspector!

在ES2015中,你可以使用类干净地做到这一点:

class NotImplemented extends Error {
  constructor(message = "", ...args) {
    super(message, ...args);
    this.message = message + " has not yet been implemented.";
  }
}

这不会修改全局Error原型,允许您自定义消息、名称和其他属性,并正确地捕获堆栈。它也很有可读性。

当然,如果您的代码将运行在旧的浏览器上,您可能需要使用像babel这样的工具。

尝试为用户定义的错误类型的每个实例创建一个新的原型对象。它允许instanceof检查像往常一样运行,并且在Firefox和V8 (Chome, nodejs)中正确报告类型和消息。

function NotImplementedError(message){
    if(NotImplementedError.innercall===undefined){
        NotImplementedError.innercall = true;
        NotImplementedError.prototype = new Error(message);
        NotImplementedError.prototype.name = "NotImplementedError";
        NotImplementedError.prototype.constructor = NotImplementedError;

        return new NotImplementedError(message);
    }
    delete NotImplementedError.innercall;
}

注意,一个额外的条目将在正确的堆栈之前。

另一种替代方法可能并不适用于所有环境。至少可以保证它在nodejs 0.8中工作 这种方法使用了一种非标准的方法来修改内部原型道具

function myError(msg){ 
      var e = new Error(msg); 
      _this = this; 
      _this.__proto__.__proto__ = e;
}