The functional inheritance mode is excerpted from javascript: thegoodparts_javascript

Source: Internet
Author: User
Javascript: the Inheritance section in thegoodparts describes a functional Inheritance method. It is not clear how to translate this functional, the purpose of this pattern is to make sure that the object attributes are truly private. We cannot directly access the object status and can only operate through privileged methods.
Here is an example.

The Code is as follows:


Var person = function (cfg ){
Var that = {};
That. getName = function (){
Return cfg. name | 'unknow name ';
};
// Default Gender: 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 language
Langs: ['javascript ', 'java', 'php']
});
Console. debug (me. getFamiliarLanguage ());
Console. debug (me. getProfile ());
Me. setStatus ('Oh really busy ..');
Console. debug (me. getStatus ());


From the code, we can see that new is useless when calling the programmer method, and this keyword does not appear in the method.
If an attribute value assignment code similar to this. name = cfg. name is displayed and called with new (the constructor call method), the name attribute of the generated object is no longer private. For example:

The Code is as follows:


// The first letter of the method name is capitalized to indicate that the method is called with new as a constructor.
Var Person = function (cfg ){
This. name = cfg. name;
This. gender = cfg. gender;
}
// New generates a person1
Var person1 = new Person ({
Name: 'Andrew ',
Gender: 'male'
});
// Originally, we wanted to make name and gender private and use a method similar to setter getter to read and write data, just like javaBean.
Alert (person1.name); // 'Andrew'
Alert (person1.gender); // 'male'


From the above example, we can see that the attributes of person1 can be accessed directly, but not truly private. sometimes we use code standards or conventions to indicate that a property we define is private, such as this. _ name indicates that the name attribute is private. I personally think that as long as the Conventions are unified, this is also a good method. In a third-party js library, there may be many such applications, such as YUI2
Continue to look at the initial code. Instead of using this, we use that as the carrier. From the programmer method, we can see the role that played by that. After calling person, the returned "that" already has the "getName" and "getGender" methods of "person". Then, based on the specific needs of programmer, we can extend the "that" method. Of course, we can override the original method, share in programmer can be used to centralize some private variables and Methods. Through the javascript scope and closure mechanism, you can process and call them in that extension method, such as that in the code. getStatus and that. setStatus method, and finally return that.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.