I was developing a small JS library and was to use there custom Error exceptions.
So, I decided to inherit them (ORLY?) from native Javascript Error object in this way:
var MyError = function(){
Error.prototype.constructor.apply(this, arguments);
};
// Inherit without instantiating Error
var Surrogate = function(){};
Surrogate.prototype = Error.prototype;
MyError.prototype = new Surrogate();
// Add some original methods
MyError.prototype.uniqueMethod = function(){...}
Looks like usual inheritance.. But!
var err = new MyError("hello! this is text!");
console.log(err.message); // is giving me empty string!
I have read articles about this here and on MDN, but all of them saying me to use something like this:
var MyError = function(msg){
this.message = msg;
};
But my question is - if not in the constructor of Error, then where message is initialized? May be somebody knows how Error native constructor works?
Thanks!
P.S. If it interesting - I was developing Enum.js library.