The role of this mode is to ensure that the object properties are truly privatized. We cannot directly access the state of the object and can only operate through privileged methods.
Let’s take an example directly.
var person = function(cfg) {
var that = {};
that .getName = function() {
return cfg.name || 'unknow name';
};
// Gender default male
that.getGender = function() {
return cfg .gender || 'male';
};
return that;
};
var programmer = function(cfg) {
var that = person(cfg),
share = {};
share.status = 'normal';
that.getFamiliarLanguage = function() {
return (cfg.langs || []).join(' ');
} ;
that.getProfile = function() {
return 'hi,my name is ' that.getName();
};
that.getStatus = function() {
return share .status;
};
that.setStatus = function(status) {
share.status = status;
};
return that;
};
var me = programmer({
name: 'AndyZhang',
gender: 'male',
// Familiar languages
langs: ['javascript', 'java', 'php']
});
console.debug(me.getFamiliarLanguage());
console.debug(me.getProfile());
me.setStatus('oh really busy..');
console.debug(me.getStatus());
As you can see from the code, when we call the programmer method, we do not use new, and the this keyword does not appear in the method.
If there is an attribute assignment code like this.name = cfg.name, and then use new to call it (constructor calling method), the name attribute of the generated object will no longer be private. For example:
// The first letter of the method name here is capitalized to indicate that it is a constructor using new Call
var Person = function(cfg) {
this.name = cfg.name;
this.gender = cfg.gender;
}
// new person1
var person1 = new Person({
name: 'Andrew',
gender: 'male'
});
// Originally I wanted to make name and gender private and use similar setter getter methods. Read and write just like javaBean
alert(person1.name); // 'Andrew'
alert(person1.gender); // 'male'
From the above example you can It can be seen that we can directly access the attributes of person1, but they are not truly private. Sometimes we will use code specifications or conventions to indicate that a certain attribute we define is private. For example, this._name is written in this way to represent name. Properties are private. Personally, I think this is a good method as long as the agreement is unified. In third-party js libraries, this method may be used more often, like YUI2
Continue to look at the initial code, this is not used , but uses that as a carrier. From the programmer method, we can see the role of that. After calling person, the returned that already has the methods getName and getGender that person has. Then we based on the specific needs of the programmer. Expand on the basis of that, of course, you can also overwrite the original method. Share in programmer can be used to centralize some private variables and methods. Through the scope and closure mechanisms of JavaScript, they can be processed and called in the method extended by that. , such as the that.getStatus and that.setStatus methods in the code, and finally return that.