While working with javascript object I came to this code :
var mainModule = {
opt : {
opt1: 'my option1',
opt2: 'my option2',
opt3: 'my option3',
},
init: function(options){
jQuery.extend(this.opt, options);
this.mainMethod();
},
mainMethod: function() {
//do Stuff
var color = this.opt.opt1;
},
secondaryMethod1: function(){/*stuff*/},
secondaryMethod2: function(){/*stuff*/},
secondaryMethod3: function(){/*stuff*/},
secondaryMethod4: function(){/*stuff*/},
thirdlyMethod1: function(){/*stuff*/},
thirdlyMethod2: function(){/*stuff*/},
thirdlyMethod3: function(){/*stuff*/},
thirdlyMethod4: function(){/*stuff*/},
};
With this code I often check the opt object with this.opt
as this
is mainModule.
But all the code begin to be a littre messy with all the different method
so I ended with this new code whith a new level of depth in the main object.
var mainModule = {
opt : {
opt1: 'my option1',
opt2: 'my option2',
opt3: 'my option3',
},
init: function(options){
jQuery.extend(this.opt, options);
this.mainMethod.init();
},
mainMethod: {
init: function() {
//do Stuff
var color = mainModule.opt.opt1;
},
other: function(){},
functions: function(){},
here: function() {}
},
secondary: {
method1: function(){/*stuff*/},
method2: function(){/*stuff*/},
method3: function(){/*stuff*/},
method4: function(){/*stuff*/},
}
thirdly: {
Method1: function(){/*stuff*/},
Method2: function(){/*stuff*/},
Method3: function(){/*stuff*/},
Method4: function(){/*stuff*/},
}
};
But with this new one I can't use this.opt
because this
isn't the mainModule anymore.
With this kind of object, is there a better way to retrieve the opt object ? Does this new level of depth is necessary or should I use maybe a pseudo namespace ?