I noticed a strange behavior while defining custom error objects in Javascript:
function MyError(msg) {
Error.call(this, msg);
this.name = \"MyError\
function MyError(msg) {
var err = Error.call(this, msg);
err.name = "MyError";
return err;
}
Error doesn't manipulate this, it creates a new error object which is returned. That's why Error("foo") works aswell without the new keyword.
Note this is implementation specific, v8 (chrome & node.js) behave like this.
Also MyError.prototype.__proto__ = Error.prototype; is a bad practice. Use
MyError.prototype = Object.create(Error.prototype, {
constructor: { value: MyError }
});