Node.js模块继承

时间:2014-09-06 21:58:42

标签: javascript node.js inheritance

当我的父类具有我不会再为我的子类指定的依赖项时,我在应用程序中正确定义继承时遇到问题。

这是我的设置:

parent.js

var foo = require('foo');

function Parent() {};

Parent.prototype.getFoo = function() {
    return foo;
};

module.exports = Parent;

child.js

var util = require('util');
var Parent = require('./parent');

function Child() {
    Parent.call(this);
}

util.inherits(Child, Parent);

module.exports = Child;

在我的应用中,如果我致电child.getFoo(),它将返回undefined,除非我在var foo = require('foo')课程中声明Child

有没有办法避免这种冗余?我想在父类中只设置一次依赖项,并避免将粘贴代码复制到从其继承的任何其他类。

0 个答案:

没有答案
相关问题