ECMAScript Inheritance: The implementation of inheritance----inherited the actual method, the most important is to use the prototype chain
1. Prototype chain inheritance
The basic idea of a prototype chain is to inherit from another reference type by using a prototype to inherit the properties and methods of the other.
1 functionsupertype () {2 This. Protype =true;3 }4 5SuperType.prototype.getSuperValue =function() {6 return This. Protype;7 }8 9 functionsubtype () {Ten This. Subproperty =false; One } A -Subtype.prototype =Newsupertype ();//Inheritance is an example of supertype - theSubType.prototype.getSubValue =function() { - return This. Subproperty; - } - + varInstance =Newsubtype (); - +Console.log (Instance.getsupervalue ());//true
Note the following points:
(1) The default prototype for all custom objects is an instance of object, and all functions have a prototype [[[prototype]] pointer pointing to object.
(2) Determine the relationship between the prototype and the instance method: Instanceof and isPrototypeOf ()
(3) The method to be added to the prototype must be after replacing the prototype
(4) Using the object literal method can easily cut off the connection between prototypes and inherited prototypes
Prototype inheritance issues:
A. The prototype contains a reference type value that inherits from the prototype
B. Parameters cannot be passed to a superclass's constructor
2. Borrowing constructors (forged functions and classic inheritance)
Purpose: To solve the problem of the inclusion reference type of the prototype inheritance
1 functionsupertype () {2 This. colors = [' A ', ' B ']; 3 }4 5 function subtype () {
Inherit Supetype6 Supertype.call (this); After this definition, the instance of subtype is a copy of the instance of color7 }8 9 var instance = new Subtype ();Ten One Instance.colors.push ("C"); A Console.log (instance.colors);//' A ', ' B ', ' C ' - - var Instance1 = new subtype (); theConsole.log (instance1.colors); ' A ', ' B '
The biggest advantage of using borrowed constructs is that subclasses can pass parameters to the superclass, and the biggest disadvantage is that the method cannot inherit
3. Combining Inheritance---Prototype chain and borrowing constructor inheritance combinations
Combinatorial inheritance is the most common way of inheriting in JavaScript
1 functionsupertype (name) {PE2 This. Name =name;3 This. color = [' green ', ' yellow ';] 4 }5 6SuperType.prototype.sayName =function() {7Alert This. Name); 8 }9 Ten fucntion Subtype (name, age) { OneSupertype.call ( This, name);//borrowing constructors A This. Age =Age ; - } - theSubtype.prototype =Newsupertype (); -SubType.prototype.constructor =subtype; -SubType.prototype.sayAge =function() { -Alert This. Age); + }; - + varInstance =NewSubtype (' Frank ', 29); A instance.sayname ();//' Frank ' atinstance.color.push[' Red ']; ' Red ', 'Green ', ' yellow '
24
25
- var New subtype (' Jack ', 24
- instance1.sayage ();//24
alert (instance1.color);//' green ', ' yellow '
4. prototype-Type Inheritance
The principle is the following function:
1 function Object (o) {2 function F () {}; 3 F.prototype = o; 4 return New F (); 5 }
ECMAScript implements the above method with the addition of the object.create () function, which accepts two functions, one that is the prototype of the new object and a new property that can optionally be added to the new object.
The archetypal inheritance purpose is to inherit the properties and methods of another object only for one object, without the need to define a constructor. However, when the method object contains reference types, all object instances are shared.
5. Parasitic inheritance
6. Parasitic combined inheritance
JavaScript---inherited