I am trying to understand the JavaScript Module Pattern. I\'ve seen examples of what it should look like, but I don\'t understand how to use it.
For example, a few thing
Unlike JAVA, Javascript does not have the concept of private and public on properties or on methods. Let’s create an object called person which has 2 properties firstName and lastName, and also create 2 functions which will be getters for our properties. In Javascript we can create functions as properties of objects & those functions are accessible like any other property anywhere.
var person={
"firstName":"Anoop",
"lastName":"Rai",
"getFirstName":function(){
return this.firstName;
},
"getLastName":function(){
return this.lastName;
}
};
console.log(person.getFirstName());
console.log(person.firstName);
As expected above codes 11 prints Anoop and Anoop. Hmmm, that’s not good for object oriented programming. Well, we successfully implemented getters and so we should also have setters that should be of public scope and properties should be marked as private scope (what??? these private and public concept belongs to JAVA, C++ like languages). Our intentions are good, lets apply concepts specific to Javascript. We don’t want to do person.firstName, we want the prevent the access of the property directly. In languages like JAVA because of private and public we achieved the controlled acccess of properties, but in Javascript everything is public.
Javascript uses concept of closures to implement things like private & public. This pattern is called Module Pattern. That is, hiding variables from public access. In order to implement scopes, wrap your codes in a function (remember, scopes are implemented via functions in Javascript).
function createPerson(){
var returnObj={
"firstName":"Anoop",
"lastName":"Rai",
"getFirstName":function(){
return this.firstName;
},
"getLastName":function(){
return this.lastName;
}
};
return returnObj;
}
var person=createPerson();
console.log(person.getFirstName());
console.log(person.firstName);
Now also above lines prints Anoop and Anoop. Still no success. As long as properties are tied to the object it will be accessed directly. Let’s untie it. Instead of properties we make variables of function scope (closure variables).
function createPerson(){
var firstName="Anoop";
var lastName="Rai";
var returnObj={
"getFirstName":function(){
return firstName;
},
"getLastName":function(){
return lastName;
}
};
return returnObj;
}
var person=createPerson();
console.log(person.getFirstName());
console.log(person.firstName);
Now above lines prints Anoop and undefined. How does this happen? Because of closures, when the functions getFirstName and getLastName was created the functions had the whole scope chain or say pointers to the relevant variables i.e. firstName and lastName. The object returnObj does not remember the variabes but the function objects remembers, because of closures. Looks like we achieved what we wanted to, but one thing is left and that is setters for the controlled access of firstName and lastName. Let’s implement setters.
function createPerson(){
var firstName="Anoop";
var lastName="Rai";
var returnObj={
"getFirstName":function(){
return firstName;
},
"getLastName":function(){
return lastName;
},
"setFirstName":function(name){
return firstName=name;
},
"setLastName":function(name){
return lastName=name;
}
};
return returnObj;
}
var person=createPerson();
console.log(person.getFirstName());
person.setFirstName("Kumar");
console.log(person.getFirstName());
We successfully modified firstName but in a controlled manner.
http://jkoder.com/the-module-pattern-javascript-way-of-hiding-data-in-objects-from-public-access/