节点模块继承

时间:2014-02-26 18:07:07

标签: javascript node.js inheritance

我为ExpressJS应用程序编写了下面列出的模块。我现在需要创建一个类似的模块,其中包含大约3个已更改的方法和一些不同的实例变量。我的计划是创建一个具有所有公共(称为Common.js)的超类,然后将其用于两个或更多子类。

我将指向教程的指针推广可能对我有帮助,但这是我的具体问题:

  • 需求会很常见,我想我把它们放在Common.js中, 正确?
  • 我假设我应该尽可能多地将实例变量(子类)推广到Common?
  • 以下可能是子类的模板,Object.create位于文件的顶部

SubClass片段:

var Common = require("./Common");   
SubClass.prototype = Object.create(Common.prototype);

SubClass.prototype.subMethod = function() {....}

我还假设任何子方法都可以引用超类中的变量,以及子类中的新变量,如this.variableName,

BTW,我将如何创建新的subClass实例变量?

这是我原来的代码:

var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var tools = require("../tools/tools");
var Job = require("./falconJob");

var Batch = function (ticket) {
    this.counts = [];
    this.maxes = [];
    this.errors = [];
    this.done = [];
    this.jobs = 0;
    this.started = Date.now();
    this.ended = Date.now();
    this.jobBatch = {};
    this.ticket = ticket;
    this.batchRoot = null;
}

Batch.prototype.setup = function (frameList, req, next) {
    this.group(frameList);
    this.makeRoot(req, next);
}

Batch.prototype.group = function (list) {
    _.forEach(list, function (obj) {
        if (this.jobBatch[obj.type] == undefined) {
            this.jobBatch[obj.type] = [];
        }
        this.jobBatch[obj.type].push(obj);
    }, this);
};

Batch.prototype.makeRoot = function (req, next) {
    var config = global.app.settings.config;
    this.batchRoot = path.join(config.JobsPath, this.ticket);
    var self = this;
    fs.mkdir(this.batchRoot, function (err) {
        if (err) return next(err);
        var mapInfoFile = path.join(self.batchRoot, "MapInfo.json");
        var mapInfo = {
            Date: (new Date()).toISOString(),
            Version: global.manifestVID,
            Zoom: req.body.Zoom,
            CenterLat: req.body.CenterLat,
            CenterLon: req.body.CenterLon
        };
        fs.writeFile(mapInfoFile, tools.pretty(mapInfo), function (err) {
            if (err) return next(err);
            return next(null);
        });
    });
};

Batch.prototype.spawn = function () {
    _.forEach(this.jobBatch, function (files, key) {
        var job = new Job(key, files, this.batchRoot, this.ticket, this);
        this.begin(job);
        job.exec();
    }, this);
};

Batch.prototype.count = function () {
    var sum = 0;
    for (var key in this.counts) {
        sum += this.counts[key];
    }
    return sum;
}

Batch.prototype.total = function () {
    var sum = 0;
    for (var key in this.maxes) {
        sum += this.maxes[key];
    };
    return sum;
}

Batch.prototype.fails = function () {
    var sum = 0;
    for (var key in this.errors) {
        sum += (this.errors[key]) ? 1: 0;
    };
    return sum;
}

Batch.prototype.finished = function () {
    var keylist = Object.keys(this.done);
    if (keylist.length == 0) return false;
    for (var key in this.done) {
        if (this.done[key] == false) return false;
    };
    if (this.jobs != 0) return false;
    return true;
}

Batch.prototype.rate = function () {
    var speed = (this.count() * 1000) / (this.ended - this.started); // tiles / second
    return speed;
}

Batch.prototype.begin = function (job) {
    var type = job.type;
    this.jobs++;
    this.counts[type] = 0;
    this.maxes[type] = 0;
    this.errors[type] = false;
    this.done[type] = false;
}

Batch.prototype.end = function (job) {
    type = job.type;
    this.jobs--;
    this.errors[type] = job.errors;
    this.done[type] = true;
}

Batch.prototype.update = function (status) {
    type = status.layer;
    this.ended = Date.now();
    this.counts[type] = status.tilesCount;
    this.maxes[type] = status.tilesMax;
    this.done[type] = status.done;
}

module.exports = Batch;

1 个答案:

答案 0 :(得分:0)

我很惊讶,没有人回答。好吧,我有一个解决方案和一些提示。首先,阅读有关javascript继承简介的Mozilla开发人员页面:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

以下是我如何构建我的"子模块"我可以只需要它,它将拉入超级模块,然后将其子类化。

var _ = require('lodash'); // require any additional modules that your sub module needs
var BatchRoot = require('./Batch'); // require the super-module with the superclass
var Job = require("./falconJob"); // another module that I need

var Batch = function (ticket) {
    BatchRoot.call(this, ticket);    // The superclass constructor takes "ticket" as a param
// define new subclass instance variables here, e.g. this.foobar = 33;
}

Batch.prototype = new BatchRoot();         // This does the subclassing
Batch.prototype.constructor = BatchRoot;   // MDN says to do this to correct the constructor pointer because it points to Batch


// this is a new subclass function, notice that I use Job which is only defined here

Batch.prototype.spawn = function () {
    _.forEach(this.jobBatch, function (files, key) {
        var job = new Job(key, files, this.batchRoot, this.ticket, this);
        this.begin(job);
        job.exec();
    }, this);
};

module.exports = Batch;