How to Implement inheritance in js (five inheritance methods in js) and five inheritance methods in js
Js inheritance has five implementation methods:
1. The first method of inheritance: Object impersonating
Function Parent (username ){
This. username = username;
This. hello = function (){
Alert (this. username );
}
}
Function Child (username, password ){
// Use the following three lines to append the Parent attributes and methods to the Child to inherit
// Step 1: this. method is used as a temporary property and points to the object pointed to by the Parent,
// Step 2: execute this. method to execute the object function pointed to by the Parent.
// Step 3: destroy the this. method attribute, that is, Child has all the attributes 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 method: call () method
The call method is a method in the Function class.
The value of the first parameter of the call method is assigned to this
The second parameter of the call method is assigned to the parameters accepted by the class (that is, the method) in turn.
Function test (str ){
Alert (this. name + "" + str );
}
Var object = new Object ();
Object. name = "zhangsan ";
Test. call (object, "langsin"); // at this time, the first parameter value object is passed to this, the second parameter "langsin" is assigned to the str of the test class (that is, the method ).
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 method of inheritance: apply () method
The apply method accepts two parameters,
A. the first parameter is the same as the first parameter of the call method, that is, this parameter is assigned to the class (that is, the method ).
B. The second parameter is of the array type. Each element in the array is assigned to the parameters accepted by the class (that is, the method) in turn.
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 method of inheritance: The prototype method, that is, the subclass appends all attributes and Methods appended by prototype to Child in the parent class through prototype, thus implementing inheritance.
Function Person (){
}
Person. prototype. hello = "hello ";
Person. prototype. sayHello = function (){
Alert (this. hello );
}
Function Child (){
}
Child. prototype = new Person (); // This line is used to append all attributes and Methods appended by prototype to Child in the Parent, thus implementing inheritance.
Child. prototype. world = "world ";
Child. prototype. sayWorld = function (){
Alert (this. world );
}
Var c = new Child ();
C. sayHello ();
C. sayWorld ();
5. The fifth method of inheritance: Hybrid Method
Mixed call mode and prototype chain mode
Function Parent (hello ){
This. hello = hello;
}
Parent. prototype. sayHello = function (){
Alert (this. hello );
}
Function Child (hello, world ){
Parent. call (this, hello); // inherits the attributes of the Parent class.
This. world = world; // New Attributes
}
Child. prototype = new Parent (); // inherit the method of the Parent class
Child. prototype. sayWorld = function () {// Add some methods
Alert (this. world );
}
Var c = new Child ("zhangsan", "lisi ");
C. sayHello ();
C. sayWorld ();