【发布时间】:2017-04-12 08:43:08
【问题描述】:
直到现在我才考虑使用 RequireJS 和 AMD 模块。 到目前为止 - 所有的事情都是通过几个全局变量和自调用函数来管理的。
例如,我的模块的外观:
function HugeModule() {
//usage = new HugeModule();
};
HugeModule.prototype.functionX = function() {
//Lets say - around 50 functions for HugeModule prototype
};
HugeModule.SubModule = function() {
//usage = new HugeModule.SubModule();
//And here could be multiple subModules like this
};
HugeModule.SubModule.prototype.functionX = function() {
//Lets say - around 20 functions for HugeModule.SubModule prototype
};
现在我会这样写,我会把它分成至少 4 个文件:
//HugeModule.js
var HugeModule = (function() {
function HugeModule() {
//usage = new HugeModule();
};
return HugeModule;
})();
//HugeModule.somePrototypeFunctions.js
(function() {
HugeModule.prototype.functionX = function() {
//Lets say - around 50 functions for HugeModule prototype
};
})();
//HugeModule.SubModule.js
(function() {
HugeModule.SubModule = function() {
//usage = new HugeModule.SubModule();
//And here could be multiple subModules like this
};
})();
//HugeModule.SubModule.someOtherPrototypeFunctions.js
(function() {
HugeModule.SubModule.prototype.functionX = function() {
//Lets say - around 20 functions for HugeModule.SubModule prototype
};
})();
我真的很想用 AMD 模块和 RequireJS 编写这些模块,我对它们应该如何编写有一个基本的想法,但我不确定 - 我将如何在多个模块之间拆分它们。
我可以这样写:
define([], function() {
function HugeModule() {
//usage = new HugeModule();
};
HugeModule.prototype.functionX = function() {
//Lets say - around 50 functions for HugeModule prototype
};
return HugeModule;
});
但我想在多个文件之间拆分它。我不希望使用连接文件的构建工具。
我想要的是一个必需的模块 - HugeModule,它将解决 HugeModule.somePrototypeFunctions 和 HugeModule.SubModule 的所有依赖关系(这将解决 HugeModule.SubModule.someOtherPrototypeFunctions 的依赖关系)
我应该如何解决这个问题?
【问题讨论】:
标签: javascript requirejs amd