As I understand it, you want to have lexical variables that all of your modules close, but you want to save the code for different modules in different files.
One way to achieve this precise behavior is to create one Javascript file by combining the module definitions together:
Header.js-partial
var myObject = (function(){ var mainObjectList = []; var globalObject = { init:function(mainObjectId){
Logger.js:
function Logger() { this.enabled = true; } Logger.prototype.log = function() { if (window.console && window.console.log) { return window.console.log.apply(window.console.log, arguments]); } }
etc..
Add other module files as desired. They can refer to lexical variables.
Footer.js-partial
}// end init }; // end globalObject return globalObject;
}) ();
In the end you will need a script that will merge all these files together into a single js file. There is no other way to access a lexical variable in pure Javascript.
Great alternative
However, this technique is ripe for confusion, and I do not recommend it. Closing is usually intended to close on top of local variables, not program variables. If you use lexical variables declared 1000 lines back, you will spend some amount of time tracking where all your variables were declared at the start of your program. Instead, you should probably globalObject private "global" data in a globalObject . For example, save mainObjectList in globalObject.mainObjectList and specify that in the module files.
I recommend a solution similar to that described by nemophrost. Each file must be valid javascript on its own.
gonzojive
source share