Chaining JS modules #2
by toubia95
CSS
/*
source : http://toddmotto.com/mastering-the-module-pattern/
*/
JavaScript
/**
* Revealing Module Pattern
* tout ce qui est public est déclaré dans une return{} final
*/
var module = (function() {
//Private Property (prefixed)
var _toto = 'HelloWorld';
//Public Property
var toto = 'HelloWorld2';
//Public Method
var HelloWorld = function() {
// Local private property is accessible
alert("1/ _toto = "+_toto);
// Local public property is accessible
alert("1/ module.toto = "+module.toto);
// Local private method is accessible
_Test("1/ Private Method Test 1");
};
//Private Method (prefixed)
var _Test = function(x) {
alert(x);
}
//What goes out of this closure
return {
toto: toto,
HelloWorld: HelloWorld
}
})();
var module2 = (function (module) {
//Private Property
var _titi = 'titi';
module.extension = function () {
// module1 public method is accessible
alert("2/ module.toto = "+module.toto);
// Local private property is accessible
alert("2/ _titi = "+_titi);
// Local private method is accessible
_Test2("2/ Private Method Test 2");
// module1 private property is not accessible
/* alert("2/ _toto = "+_toto); */
// module1 private method is not accessible
/* _Test("2/ Private Method Test 1"); */
};
//Private Method
var _Test2 = function(x) {
alert(x);
}
return module;
})(module || {});
module.HelloWorld();
module.extension();