![](/img/trans.png)
[英]Accessing the `__proto__` property isn't getting what I would expect it to get Javascript
[英]How would I rewrite without __proto__
我正在使用Node.JS和ExpressJS 。 以下代码用于通过我自己的消息扩展Errors对象,并且效果很好,但是我知道__proto__
是非标准的。
如果没有__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;
使用Object.create()
制作新的原型对象,然后重新添加不可枚举的construtor
属性。
AccessDenied.prototype = Object.create(Error.prototype, {
constructor: {
value: AccessDenied,
writeable: true,
configurable: true,
enumerable: false
}
});
或者,如果您不关心constructor
属性:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.