Some of the previous blogs mentioned that the implementation of JS inheritance principle, mainly about a realization mechanism, based on the prototype chain; in fact, it is a "scope" of the way (based on the "Scope " of the inheritance, is a personal summary of the statement, but let everyone understand that the mechanism of inheritance, mainly rely on, Change the function scope to implement calls to other functions).
So summed up the JS inherits the main 3 ways are as follows:
2, based on the prototype chain; (based on prototype)
3, Mixed Mode ( based on the scope call method. Based on the prototype chain)
The three categories are divided into the following 5 small categories as follows:
1, inherit the first way: Object posing
function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Inheritance is implemented by appending the properties and methods of the parent to child in the following 3 rows
The first step: This.method is as a temporary property and points to the object that the parent points to,
Step two: Execute the This.method method, which is the object function that the parent points to
Step three: Destroy the This.method attribute, which means that now child has all the properties and methods of the parent
This.method = Parent;
This.method (username);//the most critical line
Delete This.method;
This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();
2, inherit the second way: Call () method mode
The call method is a method in a function class
The value of the first parameter of the call method is assigned to the this that appears in the class (that is, a method)
The second argument of the call method begins by assigning the parameter to the class (that is, the method) that is accepted
function Test (str) {
Alert (THIS.name + "" + str);
}
var object = new Object ();
Object.name = "Zhangsan";
Test.call (Object, "Langsin");//At this point, the first parameter value object is passed to this in the test class (that is, the method), and the second parameter "Langsin" is assigned to the test class (that is, the method) str
function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Parent.call (This,username);
This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();
3, the Third way of inheritance: Apply () method mode
The Apply method accepts 2 parameters,
A, the first parameter is the same as the first parameter of the call method, which is the value that appears in the class (that is, the method).
B, the second parameter is an array type, and each element in the arrays is assigned to a parameter that is accepted by the class (that is, the method)
function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Parent.apply (this,new Array (username));
This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();
4, the fourth way of inheritance: The prototype chain mode,That is, subclasses append all properties and methods appended by prototype in the parent class to child through prototype, thus implementing the inheritance
function person () {
}
Person.prototype.hello = "Hello";
Person.prototype.sayHello = function () {
alert (This.hello);
}
function Child () {
}
Child.prototype = new Person (),//The function of this line is to append all the properties and methods appended by prototype to child in the parent, thus implementing the inheritance
Child.prototype.world = "World";
Child.prototype.sayWorld = function () {
alert (This.world);
}
var C = new Child ();
C.sayhello ();
C.sayworld ();
5, the fifth way of inheritance: Mixed mode
Hybrid call mode, prototype chain mode
function Parent (hello) {
This.hello = Hello;
}
Parent.prototype.sayHello = function () {
alert (This.hello);
} 
function Child (Hello,world) {  
Parent.call (This,hello);//Inherit the properties of the parent class  
this.world = world;//new properties  
} 
Child.prototype = new Parent ()//Inherit the method of the parent class  
Child.prototype.sayWorld = function () {//New method  
alert (This.world);  
} 
var C = new Child ("Zhangsan", "Lisi");  
C.sayhello (); C.sayworld ();
This article is from the "7439523" blog, please be sure to keep this source http://7449523.blog.51cto.com/7439523/1576266
There are 5 ways to implement JS inheritance