The Design pattern below for a JavaScript widget, is inspired by the pattern used for YUI-Framework widgets. I have made a copy for future projects. Some of the Features of this pattern are.
- Constructor by calling object.init().
- Public and Private Variables.
- Public and Private Function.
- Encapsulated in a namespace.
//repeat below line only once to create the Namspace
var NameSpace = function (){//empty add properties later}
//widget model.
NameSpace.Name = function()
{
// Private Variable for the module. Not accessible from outside
// Private function. Not accessible from outside
return {
//public properties
return this;
//public functions
init : function(){ // Priviledged method. Can be called from outside
// Here comes the initialisation code }, //end int function
}; //end of return
}(); //end of module