Javascript learning notes 9 prototype encapsulation inheritance

Source: Internet
Author: User

Well, let's build it step by step. First, let's take a look at the original method of inheritance:
Copy codeThe Code is as follows:
<Script>
Var Person = function (name, age ){
This. name = name;
This. age = age;
}
Person. prototype. SayHello = function (){
Alert (this. name + "," + this. age );
};
Var Programmer = function (name, age, salary ){
Person. call (this, name, age );
This. salary = salary;
};
Programmer. prototype = new Person ();
Var Programmer = new ("kym", 21,500 );
Pro. SayHello ();
</Script>

We can see that in fact, the root of inheritance lies in this step Programmer. prototype = new Person (). That is to say, add the Person to the prototype chain. This point has been explained in detail in Javascript study note 7-the principle of prototype chain.
That is to say, the key to our implementation lies in the creation of the prototype chain.
In the previous article, we used JSON to create a prototype. Its prototype chain is p. _ proto __= Person. So we want to encapsulate inheritance on this, so the prototype chain should be p. _ proto _. _ proto __= SuperClass, that is, Person. _ proto __= SuperClass. However, according to the Inheritance Method of the above Code, the prototype link is Person. _ proto __= SuperClass. prototype.
This is the same as in the previous article. Our solution is to use an auxiliary function to assign the attributes in the original function to X and then assign X. prototype = SuperClass, that is, we encapsulate the sub-prototype.
Well, we can use this idea to encapsulate the inheritance relationships of the prototype chain.
Copy codeThe Code is as follows:
<Script>
Var Factory = {
Create: function (className, params ){
Var temp = function (){
ClassName. Create. apply (this, params );
};
Temp. prototype = className;
Var result = new temp ();
Return result;
},
CreateBaseClass: function (baseClass, subClass ){
Var temp = function (){
For (var member in subClass ){
This [member] = subClass [member];
}
};
Temp. prototype = baseClass;
Return new temp ();
}
};
Var People = {
Create: function (name, age ){
This. name = name;
This. age = age;
},
SayHello: function (){
Alert ("Hello, My name is" + this. name + ". I am" + this. age );
}
};
Var Temp = {
Create: function (name, age, salary ){
People. Create. call (this, name, age );
This. salary = salary;
},
Introduce: function (){
Alert (this. name + "$" + this. age + "$" + this. salary );
}
};
Var Programmer = Factory. CreateBaseClass (People, Temp );
Var pro = Factory. Create (Programmer, ["kym", 21,500]);
Pro. SayHello ();
</Script>

This completes the encapsulation of the inheritance relationship. Of course, we can also not write a variable separately:
Copy codeThe Code is as follows:
Var Programmer = Factory. CreateBaseClass (People,
{
Create: function (name, age, salary ){
People. Create. call (this, name, age );
This. salary = salary;
},
Introduce: function (){
Alert (this. name + "$" + this. age + "$" + this. salary );
}
});

Of course, this is based on my hobbies. I personally think that the first method is clearer, but the second method is more elegant.

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.