简体   繁体   English

模块导出多个类

[英]Module export multiple classes

This is /cars/ford.js 这是/cars/ford.js

                   Ford = Car.extend({
                    classId: 'Ford',

                    init: function (driver) {
                        this._driver = driver;
                        Car.prototype.init.call(this);


                    tick: function (ctx) {
                        Car.prototype.tick.call(this, ctx);
                    },

                    destroy: function () {
                        if (this._driver !== undefined) {
                            this._driver._license.pull(this);
                        }
                        Car.prototype.destroy.call(this);
                    }
                });

if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') {
                module.exports = Ford;
            }

This is /cars/knightrider.js: 这是/cars/knightrider.js:

                   KITT = Car.extend({
                    classId: 'KITT',

                    init: function () {
                    Car.prototype.init.call(this);
                    var self = this;
                    },


newDirection: function () {
    var self = this;

    this._move.direction()
        .duration(Math.random())
        .properties({
            x: Math.random(),
            y: Math.random(),
        })
        .voice('robotic')
        .afterDirection(function () {
            self.newDirection();
        })
        .start();


}
                });

if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') {
                module.exports = KITT;
            }

I want to have all cars inside the same file to preserve my self sanity. 我希望所有汽车都放在同一个文件中,以保持自我理智。 How can I wrap them without altering my classes? 如何包装它们而不改变班级? Feel free to recommend any 'proper packaging' for Javascript functions book or tutorial, because I really dislike to open files. 随意为Javascript函数书或教程推荐任何“适当的包装”,因为我真的不喜欢打开文件。 When I'm editing a car, I might want to edit other one. 当我编辑汽车时,可能要编辑其他汽车。

Wish I could do: 希望我能做:

        module.exports.Allmycars = KITT, Ford;

And then call them with: 然后致电给他们:

        Allmycars.Ford

A solution could be : 一个解决方案可能是:

//cars/index.js

module.exports = {
  KITT:require("./knightrider"),
  Ford:require("./ford")
}

//So u could use : //因此您可以使用:

var allMyCars = require("./cars");
var ford = new allMyCars.Ford();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM