Any variable defined inside a function is considered private since it is inaccessable outside that function. This includes function arguments, local variables, and functions defined inside other functions.
A privileged method is a public method that has access to private variables and/or private functions. There are two ways to create privileged methods on objects. The first is to do so inside a constructor, as in this example:
1 function MyObject(){ 2 var privateVariable = 10; 3 4 this.publicMethod = function(){ 5 privateVariable++; 6 console.log(privateVariable); 7 }; 8 } 9 10 var object1 = new MyObject(); 11 object1.publicMethod(); 12 13 var object2 = new MyObject(); 14 object2.publicMethod();
One downside, however, is that you must use the constructor pattern to accomplish this result. As discussed in Chapter 6, the constructor pattern is flawed in that new methods are created for each instance. Using static private variables to achieve privileged methods avoids this problem.
Static Private Variables
Privileged methods can also be created by using a private scope to define the private variables or functions. The pattern is as follows:
1 (function(){ 2 var privateVariable = 10; 3 MyObject = function(){}; 4 MyObject.prototype.publicMethod = function(){ 5 privateVariable++; 6 console.log(privateVariable); 7 } ; 8 })(); 9 10 var object1 = new MyObject(); 11 object1.publicMethod(); 12 13 var object2 = new MyObject(); 14 object2.publicMethod();
The main difference between this pattern and the pervious one is that private variables and functions are shared among instances.
Creating static private variables in this way allows for better code reuse through prototypes, although each instance doesn't have its own private variable. Ultimately, the decision to use instance or static private variables needs to be based on your individual requirements.
The Module Pattern
The module pattern, as described by Douglas Crockford, does the same for singletons. Singletons are objects of which there will only ever be one instance. Traditionally, singletons are created in JavaScript using object literal notation.
The module pattern augments the basic singleton to allow for private variables and privileged methods, taking the following format:
1 var singleton = function(){ 2 var privateVariable = 10; 3 function privateFunction(){ 4 return false; 5 } 6 7 return { 8 publicProperty: true, 9 publicMethod: function(){ 10 privateVariable++; 11 return privateFunction(); 12 } 13 }; 14 }(); 15 16 console.log(singleton.publicProperty); 17 console.log(singleton.publicMethod());
The module pattern is useful for cases like this, when a single object must be created and initialized with some data and expose public methods that have access to private data.
The Module-Augmentation Pattern
Another take on the module pattern calls for the augmentation of the object before returning it. This pattern is useful when singleton object needs to be an instance of a particular type but must be augmented with additional properties and/or methods. Consider the following example:
1 var singleton = function(){ 2 3 // private variables and functions 4 var privateVariable = 10; 5 function privateFunction(){ 6 return false; 7 } 8 9 // create object 10 var object = new CustomType(); 11 12 // add privileged/public properties and methods 13 object.publicProperty = true; 14 object.publicMethod = function(){ 15 privateVariable++; 16 return privateFunction(); 17 }; 18 19 // return the object 20 return object; 21 }();