我创建了一个JavaScript应用程序,所有代码都在一个文件中。该应用程序已经变得相当庞大,我认为现在是将其拆分成多个文件的时候了,但我很难弄清楚如何做到这一点。我认为问题出在我决定构建应用程序的方式上,它使用以下模板:
var myApp = function(){
//there are several global variables that all of the modules use;
var global1, global2, module1, module2;
global1 = {
prop1:1
};
//There are also several functions that are shared between modules
function globalFunction(){
}
var ModuleOne = function(){
function doSomething(){
//using the global function
globalFunction();
}
return{
doSomething:doSomething
}
};
var ModuleTwo = function(){
function doSomething(){
//module 2 also needs the global function
globalFunction();
//Use the functionality in module 1
//I can refactor this part to be loosely coupled/event driven
module1.doSomething();
}
};
module1 = new ModuleOne();
module2 = new ModuleTwo();
};
即使所有模块都是松散耦合的和事件驱动的,但我仍然不知道如何将其拆分成多个文件,因为每个模块都依赖于共享的函数/变量。有没有人有建议?