util.inherits - 替代或解决方法 [英] util.inherits - alternative or workaround

查看:152
本文介绍了util.inherits - 替代或解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是节点中的n00b,并且发现 util.inherits()非常有用,除了它似乎替换了原始对象的整个原型。例如:

I am a n00b in node, and find util.inherits() very useful, except for the fact that it seems to replace the entire prototype of the original object. For instance:

var myClass = function(name){
    this._name = name;  
};

myClass.prototype = {
    (...)
};

util.inherits(myClass, require('events').EventEmitter);

似乎抹掉了原来的原型。

seems to erase my original prototype.

这给我带来两个不便:

1 - 在调用继承后,我必须声明为我的原型添加属性,

That brings me two inconveniences:
1 - I have to declare add properties to my prototype after calling inherits,

var myClass = function(name){
    this._name = name;  
};

util.inherits(myClass, require('events').EventEmitter);

myClass.prototype.prop1 = function(){...};
myClass.prototype.prop2 = function(){...};

最重要的是,我认为我不能从两个或更多不同的类继承。

and, most important, i think i cannot inherit from two or more different classes.

任何人都可以向我解释为什么这有意义以及解决这个问题的好方法是什么?

Can anyone explain to me why this makes sense and what would be a good way to work around this?

谢谢

推荐答案

从节点版本5.0.0开始, util.inherits 已更改为使用 setPrototypeOf 方法:

As of node version 5.0.0, util.inherits has been changed to support the behaviour you are looking for using the setPrototypeOf method:

FirstBase .js

FirstBase.js

function FirstBase(firstBaseProp){
    this.firstBaseProp = firstBaseProp;
}

FirstBase.prototype.getFirstBaseProp = function(){
    return this.firstBaseProp;
};


module.exports = FirstBase;

SecondBase.js

SecondBase.js

var FirstBase = require('./FirstBase.js'),
    util = require('util');

function SecondBase(firstBaseProp, secondBaseProp){
    this.secondBaseProp = secondBaseProp;
    SecondBase.super_.apply(this, arguments);
}

SecondBase.prototype.getSecondBaseProp = function(){
    return this.secondBaseProp;
};

util.inherits(SecondBase, FirstBase);

module.exports = SecondBase;

ThirdBase.js

ThirdBase.js

var SecondBase = require('./SecondBase.js'),
    util = require('util');

function ThirdBase(firstBaseProp, secondBaseProp, thirdBaseProp){
    this.thirdBaseProp = thirdBaseProp;
    ThirdBase.super_.apply(this, arguments);
}

ThirdBase.prototype.getThirdBase = function(){
    return this.thirdBaseProp;
};

util.inherits(ThirdBase, SecondBase);

module.exports = ThirdBase;

instance.js

instance.js

var ThirdBase = require('./ThirdBase.js');

var instance = new ThirdBase('first', 'second', 'third');

// With node < 5.0.0 (Object.create)
console.log(instance.getFirstBaseProp()); // first
console.log(instance.getSecondBaseProp()); // undefined
console.log(instance.getThirdBase()); // undefined

// With node >= 5.0.0 (Object.setPrototypeOf)
console.log(instance.getFirstBaseProp()); // first
console.log(instance.getSecondBaseProp()); // second
console.log(instance.getThirdBase()); // third

如果您运行的是支持的旧版本节点setPrototypeOf (0.12.x),你只需 export util.inherits 并将其用作内部实用程序函数。

If you're running an older version of node that supports setPrototypeOf (0.12.x does), you can just export util.inherits and use it as an internal utility function.

这篇关于util.inherits - 替代或解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆