Java-Script Widget Pattern

Saturday, 23 February 2008 17:55 by Frimbob

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

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading