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.