Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点模块继承_Javascript_Node.js_Inheritance - Fatal编程技术网

Javascript 节点模块继承

Javascript 节点模块继承,javascript,node.js,inheritance,Javascript,Node.js,Inheritance,我为一个ExpressJS应用程序编写了下面列出的模块。我现在需要创建一个类似的模块,其中包含大约3个已更改的方法和一些不同的实例变量。我的计划是创建一个拥有所有公共类(称为common.js)的超类,然后将其用于两个或更多子类 我认为教程可能对我有所帮助,但以下是我的具体问题: 需求将是公共的,我想我将它们放在common.js中, 对吧? 我假设我应该将尽可能多的实例变量(子类)升级为公共变量 下面可能是子类的模板,Object.create位于文件的顶部 子类代码段: var Comm

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

我认为教程可能对我有所帮助,但以下是我的具体问题:

  • 需求将是公共的,我想我将它们放在common.js中, 对吧?
  • 我假设我应该将尽可能多的实例变量(子类)升级为公共变量
  • 下面可能是子类的模板,Object.create位于文件的顶部
子类代码段:

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

SubClass.prototype.subMethod = function() {....}
我还假设任何子方法都可以引用超类中的变量,以及子类中的新变量,如this.variableName

顺便问一下,我如何创建新的子类实例变量

这是我的原始代码:

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;

我很惊讶,没有人回答。嗯,我有一个解决办法,还有一些建议。首先,阅读有关javascript继承简介的Mozilla开发者页面:

下面是我如何构造我的“子模块”,我可以只需要它,它将拉入超级模块,然后将其子类化

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;