What is the correct way to define a custom error in JavaScript?
Searching through SO I've found about 6 different ways to define a custom error, but I'm unsure as to the (dis)advantages to each of them.
From my (limited) understanding of prototype inheritance in JavaScript, this code should be sufficient:
function CustomError(message) {
this.name = "CustomError";
this.message = message;
}
CustomError.prototype = Object.create(Error.prototype);
Errorobject, neither of which is using theObject.create()method. the other answers seem to be variations of this theme.Object.createis merely a new way of doingCustomError.prototype = new Error()-- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Object.createfor inheritance