Prototype chain:
The constructor has a prototype property pointer that points to the prototype object , and the created instance has a pointer to the prototype object , __proto__. When an instance finds a method, it is found on the instance, and is not found by __proto__ to the prototype object. If the prototype object is an instance of another type, the prototype object contains a pointer to another prototype object, and another prototype object that also contains a pointer to another constructor.
prototype even inheritance
functionSupervalue () { This. Supervalue = "Supervalue";} SuperValue.prototype.getSuperValue () {return This. Supervalue;}functionSubvalue () { This. Subvalue = "Subvalue";}subvalue.prototype=NewSupervalue ();//The prototype is replaced by the Supervalue instance, implementing the properties and methods that inherit the Supervalue. No constructor.SubValue.prototype.getSubValue=function(){ return This. Subvalue;}varInstance =NewSubvalue (); instanceinstanceofObject//true, so the reference type inherits object and is inherited through the prototype chain. InstanceinstanceofSupervalue;//trueInstanceinstanceofSubvalue;//true//as long as the prototype chain has a constructor that appears, the result returns TRUE.
So the function type is an instance of object, so instance instanceof object returns True
Problem:
Properties of all reference types are shared when inheriting through a prototype
You cannot pass parameters to a superclass's constructor.
To borrow a constructor:
Also known as the forgery object. Calling the constructor of a superclass inside a function of a subtype
functionSupervalue () { This. colors = ["Yellow", "green", "red"];}functionSubvalue () {//inherited the Supervalue.Supervalue.call ( This);}varInstance1 =NewSubvalue (); Instance1.color.push ("New"); Instance1.color//["Yellow", "green", "Red", "new"]varInstance1 =NewSubvalue (); Instance1.color//["Yellow", "green", "red"] do not share properties
Problem: Constructor of the constructor is called inside of the parent class, unable to implement function reuse
Combination Inheritance:
Using the prototype chain to implement the method inheritance, to achieve the function reuse.
Using the constructor to implement the attribute inheritance of the instance, avoiding the sharing problem of reference instance
functionSupervalue (name) { This. Name =name; This. color = ["Red", "green"];} SuperValue.prototype.sayName=function() {alert ( This. name);}functionSubvalue (age, name) {Supervalue.call ( This, name);//inheritance of properties of constructors This. Age =Age ;}//Implementing Inheritance SupervalueSubvalue.prototype =NewSupervalue ();//Restore the overridden constructorSubValue.prototype.constructor =Subvalue;//define your own methodSubValue.prototype.sayAge =function() {alert ( This. age);}varInstance =NewSubvalue ("Yang")); Instance.color.push ("Black"); Instance.color//["Red", "green", "Black"];Instance.sayname ()//the way Yang inherited it.Instance.sayage ()//24 Own MethodvarInstance2 =NewSubvalue ("he")); Instance.color//["Red", "green"] does not share reference types. Instance.sayname ()//the method that he inherits fromInstance.sayage ()//25 Own Method
Issue: Inheritance was called two times Supevalue () inside a function, a prototype object is instantiated once, the subtype masks the properties on the prototype object, and can be inherited by the parasitic combination type.
Prototype Inheritance:
function Object (obj) { funciton F (); // declares a temporary constructor F.prototype = obj; // implementing prototype Inheritance return New F (); // returns the constructor instance. }// The entire process is a shallow copy
Parasitic inheritance:
Similar to parasitic constructors and factory patterns, you create a function that is used only for the encapsulation process to enhance the object internally.
function Object (obj) { var clone = object (obj); function () { alert (haha""); } return clone;} // function Inner Enhancement object
Parasitic combination Type inheritance:
Resolves two calls to the Supervalue () function in composite inheritance. These two calls to Supervalue () create properties on the prototype object and on the new object.
functionSupervalue (name) { This. Name =name; This. color = ["Red", "green"];} SuperValue.prototype.sayName=function() {alert ( This. name);}functionSubvalue (age, name) {Supervalue.call ( This, name);//Call the parent class constructor to create the property name and color. Masking Properties on a prototype name and color This. Age =Age ;}//implementing Inheritance Supervalue, creating properties on Prototypes name and color Subvalue.prototype =NewSupervalue (); SubValue.prototype.constructor=Subvalue;subvalue.prototype.sayage=function() {alert ( This. age);}
Solution: Through the parasitic combined inheritance, by borrowing the constructor to inherit the attribute , through the prototype chain to form the inheritance method
Method: You do not have to call the superclass's constructor in order to specify a prototype of the subtype. The subclass prototype is assigned to the sub-class through the parasitic inheriting superclass.
function Inheritprototype (subtype, supertype) { var prototype = object (super.prototype); Create object = subtype; Enhanced object = prototype; Specify object}
Parasitic Combination Inheritance Code
functionSupervalue (name) { This. Name =name; This. color = ["Red", "green"];} SuperValue.prototype.sayName=function() {alert ( This. name);}functionSubvalue (age, name) {Supervalue.call ( This, name);//implementing inheritance of attributes This. Age =Age ;}//prototype chain method inheritanceInheritprototype (subtype, supertype); SubValue.prototype.sayAge=function() {alert ( This. age);}
JavaScript advanced Programming-object-oriented implementation inheritance