I'm using Node.JS and ExpressJS. The following code is used to extend the Errors object with my own messages and works well enough, but I understand that __proto__
is non-standard.
How would I rewrite the following code without the __proto__
?
var AccessDenied = exports.AccessDenied = function(message) {
this.name = 'AccessDenied';
this.message = message;
Error.call(this, message);
Error.captureStackTrace(this, arguments.callee);
};
AccessDenied.prototype.__proto__ = Error.prototype;
Use Object.create()
to make the new prototype object, and add a non enumerable construtor
property back in.
AccessDenied.prototype = Object.create(Error.prototype, {
constructor: {
value: AccessDenied,
writeable: true,
configurable: true,
enumerable: false
}
});
Or if you don't care about the constructor
property:
AccessDenied.prototype = Object.create(Error.prototype);
"use strict";
/**
* Module dependencies.
*/
var sys = require("sys");
var CustomException = function() {
Error.call(this, arguments);
};
sys.inherits(CustomException, Error);
exports = module.exports = CustomException;
var AccessDenied = exports.AccessDenied = function ( message ) { /*...*/ };
var F = function ( ) { };
F.prototype = Error.prototype;
AccessDenied.prototype = new F();