1. Prototype chain
Rarely used alone
Copy Code code as follows:
View Code
Defines the superclass class, which has an attribute property and a method Getsupervalue
Function Superclass () {
This.property = true;
}
SuperClass.prototype.getSuperValue = function () {
return this.property;
}
Define the subclass class, with a property subproperty and a method added later Getsubvalue
function Subclass () {
This.subproperty = false;
}
Subclass class Inherits Superclass class
Subclass.prototype = new superclass ();
Subclass class to add a method getsubvalue
SubClass.prototype.getSubValue = function () {
return this.subproperty;
}
To create an instance of the subclass class
var instance = new Subclass ();
Alert (Instance.getsupervalue ());
2. Determine the relationship between the prototype and the instance
The first method uses the instanceof operator to test the constructor in the instance and the prototype chain.
Copy Code code as follows:
Alert (instance instanceof Object); True, instance is an instance of object?
Alert (instance instanceof superclass); True, instance is an instance of superclass?
Alert (instance instanceof subclass); Is true,instance an example of subclass?
The second way is to test the prototype in the prototype chain using the isPrototypeOf () method
Copy Code code as follows:
Alert (Object.prototype.isPrototypeOf (instance)); True
Alert (SuperClass.prototype.isPrototypeOf (instance)); True
Alert (SubClass.prototype.isPrototypeOf (instance)); True
3. Note points for defining methods with prototype chain inheritance
Define the order of the methods:
Copy Code code as follows:
View Code
Function Superclass () {
This.property = true;
}
SuperClass.prototype.getSuperValue = function () {
return this.property;
}
function Subclass () {
This.subproperty = false;
}
Subclass Inheritance Superclass
Subclass.prototype = new superclass (); This is written first, the newly added method and the method to override the superclass are written in the back, otherwise the overridden superclass method will never be able to invoke the
Add a new method
SubClass.prototype.getSubValue = function () {
return this.subproperty;
}
How to override a superclass
SubClass.prototype.getSuperValue = function () {
return false;
}
var instance = new Subclass ();
Alert (Instance.getsupervalue ()); Fales, where the subclass instance invokes the subclass Getsupervalue () method, and masks the superclass Getsupervalue () method,
Methods that use superclass call the superclass Getsupervalue () method
Disadvantages of a prototype chain inheritance: 1 sharing a property in a superclass, 2 you cannot pass arguments to a superclass's constructor when you create a subclass. All rarely use the prototype chain alone
4. Borrowing constructors
Rarely used alone
Advantages: You can pass parameters to the superclass. Disadvantage: Functions cannot be reused, all classes use constructor mode
Copy Code code as follows:
View Code
Function superclass (name) {
THIS.name = name;
}
function Subclass () {
Superclass.call (This, "Ruiliang"); Inherits the superclass and passes parameters to the superclass
this.age = 29; Instance Properties
}
var instance = new Subclass ();
alert (instance.name); Ruiliang
alert (instance.age); 29
6. Combined inheritance
The most common inheritance pattern
Copy Code code as follows:
View Code
//Create superclass
Function superclass (name) {
This.nam e = name;
This.colors = ["Red", "Blue", "green"];
}
SuperClass.prototype.sayName = function () {
alert (this.name);
}
////Create subclass
Function Subclass (name,age) {
Superclass.call (this,name);//Inheritance Properties
THIS.A GE = age; Own Properties
}
Subclass.prototype = new Superclass ();//Inheritance method
SubClass.prototype.sayAge = function () {/ /subclass Add New Method
Alert (this.age);
};
//Use
var Instance1 = new Subclass ("Ruiliang", 30);
Instance1.colors.push ("Black");
Alert (instance1.colors);//"Red,blue,green,black"
Instance1.sayname ();//"Ruiliang"
Instance1.sayage () ;
var instance2 = new Subclass ("Xuzunan", 26);
Alert (instance2.colors);//"Red,blue,green"
Instance2.sayname ();//"Ruiliang"
Instance2.sayage ();//30 br>
7. Other Inheritance Models
Prototype inheritance, parasitic inheritance, parasitic combined inheritance