Unlike many other object oriented languages, JavaScript does not have a simple and obvious way of distinguishing between private, protected, and public properties and methods. That doesn't mean that you can't create private properties in JavaScript, it just means that the way you define them is different to the way that other languages use. Since JavaScript doesn't have classes protected properties and methods do not exist as such since that relates to what is able to be inherited when one class is based on another class.
Another difference between private properties in JavaScript and those in other languages is that private properties in JavaScript can only be accessed from private methods of the object and from some public methods (depending on where those methods are defined). Douglas Crockford gave the name "Privileged Method" to those public methods defined in such a way as to allow access to private properties.
So how exactly do we distinguish between public and private properties. It is all in the way that we define them in the object's constructor. Here's an example where both public and private properties are created.
function example(param) {
this.a = param;
var b = true;
}
Here we create three variables in the object constructor, one of them is public (this.a) and the other two (param and b) are private.
The private properties as defined are available from any code within the constructor itself. How we make the private properties available to the object's methods is to define those methods inside the constructor as well. Private methods are not going to serve any useful purpose since they too are only able to be accessed from within the constructor and so what we need to do is to define public methods that have the necessary privilege to be able to access the private properties. We do that by defining the methods inside of the constructor instead of using any of the other ways of defining the methods (as only methods defined within the constructor are privileged to be able to access the private properties.
function example(param) {
this.a = param;
var b = true;
this.getB = function() {
return b;
}
this.setB = function(x) {
b = x;
}
}
Now we can obtain (but not change) the value of the private variable example.b (which in fact cannot actually be accessed as example.b because it is private to example) by using example.getB(). We can change its value to that held in a field called newbvalue by calling example.setB(newbvalue).
Using private properties with privileged get and set methods gives us greater control over the properties. While haven't shown it in the short exampe here the set method would normally have more code in ot to validate the value prior to allowing the property to be set. That way you can validate the value the property holds everywhere that you change its value automatically rather than having some code somewhere change its value directly to something invalid and thus stuff everything else in your code which expects only certain values in the property.
All the Object Oriented JavaScript Tutorials
- What is Object Oriented JavaScript?
- The Benefits of OO JavaScript
- JavaScript's Built in Objects
- Extending Built In Objects
- Creating Objects from Existing Objects
- Creating New Objects Without Copying Existing Ones
- Dot Notation and "this"
- prototype
- Inheritance and "constructor"
- Associative Array Notation
- Create Method if Doesn't Exist
- "self" and Multiple Objects
- Defining Objects with JSON
- Namespaces
- Lazy Definition
- Extending Methods
- Copying Objects
- Private Properties and Privileged Methods
- Public Access to Private Methods
- Chaining Methods
- Singletons
