Inherited
Implement inheritance: Inherit the actual method. ECMAScript only supports the implementation of inheritance, and its implementation is mainly based on the prototype chain to achieve.
The basic idea is to use a prototype to implement a reference type that inherits the properties and methods of another reference type.
Prototype-constructor-the relationship between instances
Constructors (prototype) <-------> Prototypes (constructor) <-------instances (_PROTO_)
There is a basic pattern for implementing the prototype chain, which is roughly the following code
1 functionsupertype () {2 3 This. property =true;4 }5 6SuperType.prototype.getSuperValue =function(){7 return This. prototype;8 }9 functionsubtype () {Ten This. Subproperty =false; One } A - //inherited the supertype. - theSubtype.prototype =Newsupertype (); - -SubType.prototype.getSubValue =function(){ - return This. Subproperty; + } - + varInstance =Newsubtype (); A alert (Instance.getsupervalue ()); at - - functionsupertype () { - This. property =true; - } - inSuperType.prototype.getSuperValue =function(){ - return This. property; to } + - functionsubtype () { the This. Subproperty =false; * } $Subtype.prototype =Newsupertype ();Panax Notoginseng -Subtype.property = { theGetsubvalue:function(){ + return This. Subproperty; A }, theSomeothermethod:function(){ + return false; - } $ } $ - varInstance =Newsubtype (); -Alert (Instance.getsupervalue ());
6.3.1 does not use literal methods to create prototype content when inheriting from a prototype, because it overrides the prototype chain
The biggest problems in prototypes are:
1 reference types are worth sharing issues with
2 inheritance implemented with prototypes, because properties are common, you cannot pass parameters to a super-type constructor when creating a subtype instance.
6.3.2 Borrowing constructors
Call () apply ()
A technique called borrowing a constructor (sometimes called a forged object or classic inheritance) can be used in the process of solving a problem with a reference type value in the prototype. The basic idea of this technique is quite simple, calling the superclass constructor within the subtype constructor. Don't forget that the function
is nothing more than an object that executes code in a particular environment, so you can also execute the constructor on the newly created object by using the Apply () and call () methods,
function Supertype () { this . Colors = ["Red", "Blue", "green" function subtype () {Supertype.call ( this );} var instance = subtype (); Instance.colors.push ( black " // red Blue green black var Instance1 = subtype (); alert ( Instance1.colors); // red Blue green
Advantages of borrowing constructors over prototype mode:
· Passing parameters
For example:
function supertype (name) { this. Name = name;} function subtype () { Supertype.call (this, "Nicholas"); this. Age =;} var New // Nicholas//
Problems with borrowing constructors
If you simply borrow a constructor, you will not be able to avoid the problem with the constructor pattern------methods are defined in the constructor, so the reuse of the function is not possible.
6.3.3
Combination inheritance (combination inheritance) prototype and borrowing constructor two methods to take its long one way
Main ideas:
The inheritance of the instance properties is implemented by borrowing constructors by using the prototype to inherit the attributes and methods.
This allows the instances to have their own properties, since they inherit the attributes from the prototype.
function Supertype (name) { This. Name =name; This. colors = ["Red","Blue","Green"]; } SuperType.prototype.sayName=function () {alert ( This. Name); } function Subtype (name,age) {//InheritanceSupertype.call ( This, name); This. Age =Age ; } //Inheritance MethodSubtype.prototype=Newsupertype (); SubType.prototype.constructor=subtype; SubType.prototype.sayAge=function () {alert ( This. Age); } varInstance1 =NewSubtype ("Nicholas", -); Instance1.colors.push ("Black"); alert (instance1.colors); Instance1.sayname (); Instance1.sayage (); varInstance2 =NewSubtype ("Greg", -); alert (instance2.colors); Instance2.sayname (); Instance2.sayage
In this example, the Supertype constructor defines two properties: name and Colors. The Supertype prototype defines a method Sayname (). The subtype () constructor passes the name parameter when it calls the Supertype constructor, and then it defines its own property, age.
Then, the instance of Supertype is assigned to the prototype of subtype, and then the method Sayage () is defined on the new prototype. This allows two different subtype instances to have their own properties----including the Colors property, and the same method can be used.
Combined inheritance avoids the defects of the prototype chain and borrowing constructors, and incorporates their advantages. and instanceof and isprototypeof can also be used to identify objects that are created based on composite inheritance.
JavaScript-----------Combination Inheritance