Private Variables in JavaScript
In many programming languages, private variables are used to encapsulate data within an object, preventing external access and modification. This is a useful feature for maintaining the integrity of an object's state. However, JavaScript does not natively support private variables. Instead, developers can approximate private variables using closures. This approach allows for controlled access to these variables through accessor methods, without exposing them directly.
Using Closures to Approximate Private Variables
A common pattern to simulate private variables in JavaScript is to use closures within constructor functions. By defining variables inside a constructor, they become inaccessible from outside the function, effectively making them private. Accessor methods can then be provided to interact with these variables.
Example: Ninja Constructor
Consider the following example, which demonstrates how closures can be used to approximate private variables:
Listing 5.3. Using closures to approximate private variables
function Ninja() { // #A
var feints = 0; // #B
this.getFeints = function(){ // #C
return feints; // #c
}; // #C
this.feint = function(){ // #D
feints++; // #D
}; // #D
}
var ninja1 = new Ninja(); // #E
ninja1.feint(); // #F
assert(ninja1.feints === undefined, // #G
"And the private data is inaccessible to us."); // #G
assert(ninja1.getFeints()=== 1, // #H
"We're able to access the internal feint count."); // #H
var ninja2 = new Ninja(); // #I
assert(ninja2.getFeints()=== 0, // #I
"The second ninja object gets its own feints variable."); // #I
- #A: Defines the constructor for a
Ninja. - #B: Declares a variable inside the constructor function. Because the scope of the variable is limited to inside the constructor, it’s a “private” variable. It is used to count how many times the ninja has feinted.
- #C: Creates an accessor method for the
feintscounter. This method provides read-only access to the value. - #D: Declares the increment method for the value. The value is private, so it can only be modified through this method.
- #E: Constructs an instance of
Ninja. - #F: Calls the
feintmethod, which increments the count of the number of times that the ninja has feinted. - #G: Verifies that the variable cannot be accessed directly.
- #H: Demonstrates that the "private" variable can be changed through the provided method.
- #I: Shows that each new
Ninjaobject gets its ownfeintsvariable.
Accessing Private Variables Through Functions
While closures provide a way to simulate private variables, it's important to note that these variables are not truly private. They can be accessed through functions, not through objects. This is demonstrated in the following example:
Listing 5.12. Private variables are accessed through functions, not through objects!
function Ninja() {
var feints = 0;
this.getFeints = function(){
return feints;
};
this.feint = function(){
feints++;
};
}
var ninja1 = new Ninja();
ninja1.feint();
var imposter = {};
imposter.getFeints = ninja1.getFeints; // #A
assert(imposter.getFeints() === 1, // #B
"The imposter has access to the feints variable!"); // #B
- #A: Makes the
getFeintsfunction ofninja1accessible through theimposter. - #B: Verifies that the supposedly private variable of
ninja1can be accessed through theimposter.
This example shows that by assigning the getFeints method of ninja1 to a new imposter object, the imposter can access the feints variable. This highlights the fact that the privacy of variables is not absolute in JavaScript, as they can be accessed through the functions that enclose them.