Module Pattern

Description

Singletons are objects of which there will only ever be one instance.

The module pattern, as described by Douglas Crockford, augments the basic singleton to allow for private variables and privileged methods, taking the following format:


var singleton = function(){
    //private variables and functions
    var privateVariable = 10;
    function privateFunction(){//from w ww  . ja v  a2 s  . com
        return false;
    }
    //privileged/public methods and properties
    return {

        publicProperty: true,

        publicMethod : function(){
           privateVariable++;
           return privateFunction();
       }
   };
}();




















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions