这种情况下工作得很好:
class CustomError2015 extends Error {
constructor(message) {
super(message); // here the message is set correctly
console.log("Message: " + this.message);
}
}
throw new CustomError2015("ECMAScript 2015 class inheritance");
我预计这个会以同样的方式工作,但它没有:
function CustomError(message){
Error.call(this, message); // here the message IS NOT set
console.log("Message: " + this.message);
}
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.constructor = CustomError;
throw new CustomError("CustomError function-like inheritance");
我想知道为什么?我的代码有问题还是什么?