繁体   English   中英

没有__proto__我该如何重写

[英]How would I rewrite without __proto__

我正在使用Node.JSExpressJS 以下代码用于通过我自己的消息扩展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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM