You can always add additional methods to Error.prototype and use them later, like this (in ES6):
Error.prototype.hello = function() { console.log(`Hello from ${this.constructor.name}!`); } const a = new Error(); a.hello();
You can also completely replace the Error class as follows:
class ImprovedError { constructor() { console.log(`I am an ${this.constructor.name}!`); } }; Error = ImprovedError; const c = new Error();
Remember that replacing the Error class can completely have some adverse consequences and unpredictable results when interacting with external dependencies (libraries), since they can expect and call methods of the original Error class.
For more information about the methods and methods of the Error class, see here.
source share