It is known a Revealing Module Pattern
in javascript, and here it is a little example.
var Module = (function(){
var a = 3,
init = function(a_user){ a = a_user; },
update = function(){},
get = function(){return a},
set = function(a_user){ a = a_user; };
return {
init : init,
update : update,
get : get,
set : set
}
})();
It's good for lots of uses. But I modified it in 2 steps, and some people tell me that I've overloaded it, and my new variations take lots of memory. So I ask, if my variations are really bad, and I shouldn't use it?
Variation 1
I like to separate public and private logic. It's useful, because I separate methods, which I allow to users of my module (in the client code).
For example:
var MyModule = (function(){
var
pr = {}, // Private Scope
PU = {}; // Public Scope
pr.fetch = function(){}; // fetching data
pr.build = function(){}; // building DOM of module
pr.show = function(){}; // showing our object
pr.hide = function(){}; // hiding our object
//... Other private methods
// Public methods
PU.init = function(){
pr.fetch().build().show();
return PU; // for making chains
}
PU.update = function(){};
//... other Public methods
return PU;
})();
I know, that in usual Revealing Module Pattern
methods that are in last return
are public, but other - private. But when you calling private method there, you just type it name, and it's unknown if it is a private method of this module, or it's a global function, that's why I have separated object - pr
and PU
.
Variation 2:
Sometimes I had need to insert some of modules twice on a page (or maybe more). So, I understand that it's not a module anymore, because Module can't have instances, Module is just a bundle of related functions. But I've decided to make a Module that can generate instances of itself.
I've used my code from Variation 1
with a little fix:
var MyModule = function(){
var
pr = {}, // Private Scope
PU = {}; // Public Scope
pr.fetch = function(){}; // fetching data
pr.build = function(){}; // building DOM of module
pr.show = function(){}; // showing our object
pr.hide = function(){}; // hiding our object
//... Other private methods
// Public methods
PU.init = function(){
pr.fetch().build().show();
return PU; // for making chains
}
PU.update = function(){};
//... other Public methods
// Constructor
PU.__construct = function() {
// constructor code
return PU;
}
return PU.__construct.apply(this, arguments);
};
// Usage:
var m1 = new MyModule(/* some options*/);
m1.init();
var m2 = new MyModule(/* other options */);
m2.init();
Are these variation really bad? Should I avoid them, as some people have said to me some time earlier...