This section focuses on the issue of inheritance.
6.3 Inheritance
Many object-oriented languages, such as Java, support two ways of inheriting: interface Inheritance (only inherited method signatures) and implementation inheritance (inheriting the actual method); Because functions are not signed, only implementation inheritance can be supported in ECMAScript. The implementation of inheritance relies primarily on the prototype chain.
6.3.1 prototype chain
Basic idea: Use a prototype to have a reference type inherit the properties and methods of another reference type.
Review the relationships of constructors, prototypes, and instances: each constructor has a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object.
So, imagine that we have a prototype object equal to an instance of another type, and the resulting prototype object will contain a pointer to another prototype, and the other prototype also contains a pointer to another constructor; look at the picture and the example.
functionsupertype () { This. property =true;} SuperType.prototy.getSuperValue=function(){ return This. property;};functionsubtype () { This. Subproperty =false;} //inherited the supertype.Subtype.prototype = new supertype (); //subtype.prototype is an example of supertype, whose internal pointer naturally points to Supertype's prototype SubType.prototype.getSubvalue=function(){ return This. Subproperty; };varInstance =Newsubtype (); alert (instance.getsupervalue);
Each of the two types is defined, with its own methods and properties, and inheritance is achieved by creating an instance of supertype and assigning that instance to Subtype.prototype. The essence is to rewrite the prototype object and replace it with an instance of a new type . ( Note that it is first rewritten after creating a new instance of var instance = new Subtype ()). All the properties and methods that existed in the instance of Supertype are now present in Subtype.prototype. Attributes and methods are inherited, and methods can be added yourself.
Note that the red and green boxes are marked with a pointer inside the Subtype.prototype, pointing to the Supertype prototype; The property is located in Subtype.prototype, Because Subtype.prototype is an instance property of Supertype.
In addition, pay attention to Instance.constructor ( note that the prototype object only constructor is not Subtype.prototype.constructor) is now pointing to the supertype. Because subtype's prototype points to the prototype of another object->supertype, the constructor attribute of the prototype object points to Supertype, remembering that everything comes from a reference.
All reference types inherit object by default and are also passed through the prototype chain. So when you call Instance.tostring (), you actually call the method that you saved in Object.prototype.
There are two ways to determine the relationship between a prototype and an instance. 1) Adopt the isPrototypeOf () method using instanceof 2), the following example
Alert (Object.prototype.isPrototypeof (instance));//true
Alert (SuperType.prototype.isPrototypeof (instance));//true
Alert (SubType.prototype.isPrototypeof (instance));//true
Define the method with caution
In subclasses, it is sometimes necessary to overwrite one of the methods in a superclass or to add a method that does not exist in a super type. However, the code that adds a method to the prototype must be placed after the statement that replaced the prototype. (Note: Because if it was washed out before, the prototype is referenced elsewhere by the parent supertype.prototype). Also note that when inheriting through a prototype chain, you cannot use object literals to create a prototype method. Because it will rewrite the prototype chain, cut off the connection.
functionsupertype () { This. property =true;//Parent Class Property} SuperType.prototype.getSuperValue=function(){//the method of the parent class prototype chain return This. property; }; functionsubtype () { This. Subproperty =false; } //inherit from supertype//creating the parent class instance to the subclass of the prototype chain (XXXX)Subtype.prototype =Newsupertype (); //New Method AddedSubType.prototype.getSubValue =function (){ return This. Subproperty; }; //override existing method in overriding a super typeSubType.prototype.getSuperValue =function (){ return false; }; varInstance =Newsubtype (); Alert (Instance.getsupervalue ()); //false
Problems with the prototype chain
Issue 1: A prototype property with a reference type value in the Front Street is shared by all instances. In the case of inheritance through a prototype, the prototype will actually program another instance of the type.
functionsupertype () { This. colors = ["Red", "Blue", "green"]; } functionsubtype () {}//inherit from SupertypeSubtype.prototype =Newsupertype (); varInstance1 =Newsubtype (); Instance1.colors.push ("Black"); alert (instance1.colors); //"Red,blue,green,black" varInstance2 =Newsubtype (); alert (instance2.colors); //"Red,blue,green,black"
Issue 2: When you create an instance of a subtype, you cannot pass parameters as a constructor of a superclass. (In fact, it should be said that there is no way to give a super-type constructor bed-bottom parameter without affecting all object instances.) So in practice, the prototype chain is seldom used alone.
The next section introduces the inheritance techniques in practice.
JavaScript Advanced Programming Chapter sixth-Object oriented section_03