Learn to create and use private variables and methods.
Variables defined within a function are private because it cannot be accessed from outside the function, but this issue can be overcome by creating closures within function, which can access these variables using a scope chain.
A privileged method is defined a public method which can access private variables/functions.
In the demo below, the methods fetchName() and assignName() are privileged and hence can access the private variable name
Another way to create privileged methods is to create a private scope and define within it private variables and functions.
In this technique, a private scope is used to enclose the constructorand its methods, public methods are defined using the prototype pattern and constructors are defined using function expression to create global functions.
In the demo below, the constructor Company has access to private variable name and methods fetchName() and assignName(), thus the variable name can be used by all instances.
The Module Pattern are created using singletons. Singletons are object which will have only one instance ever.They are generally created using object literal notation.
The module pattern is used to extend the basic singletons by add to it some private variables and privileged methods.Module patterns use an anonymous function which returns an object.