继承nodejs

时间:2016-03-11 16:29:28

标签: javascript node.js inheritance prototype

我试图在nodejs中加热模块。当我只打一次超级时,没关系,但当我开始在其他代码点调用它时,来自其中一个的数据在另一个...

RegEvent.js(超级)

var __ = require('lodash');
var RegEvent = function () {
    RegEvent.prototype.Model = new require('models/regValids')(this.model);
};

RegEvent.prototype.getAll = function () {
    var self = this;
    var args = Array.prototype.slice.call(arguments, 0);
    var callback = args.pop();
    getAll(self, args[0], args[1] || {}, args[2] || {}, callback);
};
var getAll = function (scope, company, where, projection, callback) {
    scope.Model.get(company, where, projection, function (error, result) {
        return callback(error, result);
    });
};
module.exports = RegEvent;

configPerEmployee.js

var __ = require('lodash');
var inherits = require('util').inherits;
var RegEvent = require('components/regEvent');

var ConfigPerEmployee = function () {
    this.model = 'config_employees';
    RegEvent.apply(this, arguments);
};

inherits(ConfigPerEmployee, RegEvent);

ConfigPerEmployee.prototype.handlePriority = function (company, employee, callback) {
...
}

所以,我有一个名为schedules的模块(比如configPerEmployee.js)继承RegEvents,当我从schedule调用getAll function时,我从configPerEmployee访问模型。我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

我认为问题在于,每次创建RegEvent.prototype.Model等实例时,您都需要重新分配ConfigPerEmployee

为什么不将RegEvent构造函数更改为

var RegEvent = function(modelName) {
  this.Model = new require('models/regValids')(modelName);
}

和ConfigPerEmployee等

var ConfigPerEmployee = function() {
    RegEvent.apply(this, ['config_employees']);
};