Module pattern is one of the well know design patterns in JavaScript. I did read about this pattern, for the first time, in a publication done by Douglas Crockford. Since then, I have used this pattern in many occasions in my coding.
Using a module, an interface can be presented, by hiding the implementation and the state of an object. And it's a way of having private attributes and methods. A simple implementation is given below.
var containers = (function () {
var module = {};
var default_container_width = 500; //private property
var possible_containers_list = new Array();
possible_containers_list[1]="comments";
possible_containers_list[2]="respond";
module.list = possible_containers_list; //public property
function getContainerWidth() {//private function
// ...
}
module.hideAll = function () {//public function
// ...
};
return module ;
}());
To call the public functions and to get the public property values:
containers.hideAll();
var list = containers.list;
With this pattern, the use of global variables can also be reduced.
Using a module, an interface can be presented, by hiding the implementation and the state of an object. And it's a way of having private attributes and methods. A simple implementation is given below.
var containers = (function () {
var module = {};
var default_container_width = 500; //private property
var possible_containers_list = new Array();
possible_containers_list[1]="comments";
possible_containers_list[2]="respond";
module.list = possible_containers_list; //public property
function getContainerWidth() {//private function
// ...
}
module.hideAll = function () {//public function
// ...
};
return module ;
}());
To call the public functions and to get the public property values:
containers.hideAll();
var list = containers.list;
With this pattern, the use of global variables can also be reduced.
Comments