Well, let's make it step-by-step, first let's take a look at the original wording of the inheritance:
Copy Code code 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 pro = new Programmer ("Kym", 21, 500);
Pro. SayHello ();
</script>
We see, in fact, that the root of inheritance lies in this step programmer.prototype=new person (). In other words, add person to the prototype chain. This has been explained in more detail in the JavaScript learning notes 7--prototype chain theory.
That is to say, the key to our realization is to build the prototype chain.
In the above, we use JSON to create a prototype, whose prototype chain is P.__proto__=person. So we want to encapsulate inheritance on this, then the prototype chain should be p.__proto__.__proto__=superclass, that is to say person.__proto__=superclass. But according to the inheritance method of our code above, the prototype chain relationship is Person.__proto__=superclass.prototype.
This is the same as we do in the above, our approach is to use an auxiliary function, the original function of the attribute to the X, and then let X.prototype=superclass, that is, we will be a child prototype encapsulation.
Well, in this way, we're going to implement the encapsulation of the inheritance relationship using the prototype chain.
Copy Code code 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, 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 our inheritance relationship. Of course, we can also not write a variable individually:
Copy Code code 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, it's all personal, and I think the first approach is relatively clearer, but the second approach is more elegant.