在NodeJS中创建公共类

时间:2019-05-05 19:23:36

标签: javascript node.js class

waves 因此,我想在一个文件中创建一个类,然后在另一个文件中访问它。该类充当项目中其他功能的处理程序。

主文件

"use strict";
const Cluster = require("./Handler.js");

exports = function (e) {
    // Some kinds of stuff, but I'll just skip to the class call.
    return new Cluster.Object(e); // "ReferenceError: Cluster.Object is not a constructor"
}

// Execute for testing.
exports(["foo", "bar"]);

处理程序

const Obj = require("./lib/Object.js");
const Str = require("./lib/String.js"); // Dummy

class Cluster {
    Object(e) {return new Obj(e)} // I'm sure it will happen here too, that error.
    String(e) {return new Str(e)}
}

exports = Cluster;

lib / Object

class Obj {
    constructor (e) {
        this = {
            element:  e,
            instance: instanceof e
            length:   e.length
        };
    }

    get instance() {return this.instance}
    get length()   {return this.length}


}

exports = Obj;

0 个答案:

没有答案