[英]Module export multiple classes
這是/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;
}
這是/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;
}
我希望所有汽車都放在同一個文件中,以保持自我理智。 如何包裝它們而不改變班級? 隨意為Javascript函數書或教程推薦任何“適當的包裝”,因為我真的不喜歡打開文件。 當我編輯汽車時,可能要編輯其他汽車。
希望我能做:
module.exports.Allmycars = KITT, Ford;
然后致電給他們:
Allmycars.Ford
一個解決方案可能是:
//cars/index.js
module.exports = {
KITT:require("./knightrider"),
Ford:require("./ford")
}
//因此您可以使用:
var allMyCars = require("./cars");
var ford = new allMyCars.Ford();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.