views:

36

answers:

0

Hi, I have following structure for my client;

var myObject = (function(){
    var mainObjectList = [];
    var globalObject = {
            init:function(mainObjectId){
                var logger = {};
                var utilityObject1 = {};
                var utilityObject2 = {};
                var mainObject = {};
                mainObjectList.push(mainObject);
            },//init
            comeOtherMethods:function(){}
        };//globalObject
    return globalObject;
})();

with my client I can sey myObject.init(5); and create a new structure. My problem is I have alot of utility objects inside init function closure (logger, utilityObject1, utilityObject2..). My total file exeeded 1000 lines so I want to seperate all utility objects into different files to have a better project. for example I could seperate logger, utilityObject1 , utilityObject2 to their own files. the problem is since objects are in closure I cannot just add them to main object in seperate files. so I tought of following injection method.

//start of file1
var myObject = (function(){
    var mainObjectList = [];
    var globalObject = {
            init:function(mainObjectId){
                var logger;
                var utilityObject1 = {};
                var utilityObject2 = {};
                var mainObject = {};
                mainObjectList.push(mainObject);
            },//init
            comeOtherMethods:function(){},
            injectLogger:function(creator){
                this.logger = creator();
            }
        };//globalObject
    return globalObject;
})();
//start of file2
myObject.injectLogger(function(){return {};});

That way I can seperate my files for development. but in production I can concatenate files to have one file. But I have some problems with this design. I just added an accessible injectLogger function into myObject. and my logger cannot use other local variables in closure now(I have to pass them to creator object now). My question is is there any other way to seperate that kind of code into files. (maybe an external utility.)