1 /**2 * Created by 2016/6/5.3 */4 //1. Prototype chain inheritance5 //prototype of a class, defined as an instance of a superclass to access the methods and properties of a superclass through a prototype6 functionPerson () {//Super Class7 This. Age = 40;8 This. Run =function(){9Console.log ("Person is running");Ten } One } A functionStudent () {}//sub-class -Student.prototype =NewPerson ();//Implementing Inheritance - the //Note: If you want to override or add a method to a subclass, you need to follow the inheritance. - //Cons: 1. If a value of a reference type exists in the attribute, modifying the property will be modified synchronously because the property is shared. - //2. Unable to pass parameters to the superclass constructor - //3. Unable to implement multiple inheritance + - + //2. Borrowing the constructor function A //The constructor of the superclass is called inside the constructor of the subclass. Using the Apply (obj,[]) and call (OBJ,ARG1,ARG2) functions at functionPerson () {//Super Class - This. Age = 40; - This. Run =function(){ -Console.log ("Person is running"); - } - } in - functionStudent () { toPerson.call ( This);//call the superclass constructor, where you can pass parameters + } - the varStudent =NewStudent (); *Student.run ();//"person is running" $ //Cons: 1, borrowing constructors, resulting in all methods defined in the constructor, the method cannot be reusedPanax Notoginseng //2. The methods and properties in a superclass prototype are inaccessible to subclasses, causing all types to use the constructor pattern. - the + //3. Combination Inheritance A //Pseudo-Classic inheritance combines the inheritance of prototype chains with the inheritance of borrowed constructors. Implementation of inheritance of attributes by means of constructor, and implementation of the method of prototype chain the //it realizes the reuse of methods and the independence of attributes. Combining inheritance is a common way of inheriting + functionPerson () { - This. Age = 40; $ } $Person.prototype = { - Constructor:person, -Runfunction(){ theConsole.log ("Person is running"); - }Wuyi }; the functionStudent () { -Person.call ( This);//Inheritance Properties Wu } -Student.prototype =NewPerson ();//Inheritance Method About $ //disadvantage: Because attributes and methods are inherited separately, a superclass constructor that is called two times is guaranteed. Can be optimized using parasitic combination inheritance. - - //4. Prototype-Type Inheritance - //based on prototype inheritance, similar to the inheritance of the prototype chain, this returns an instance of a subclass A functionObject (o) { + functionF () {}//Create a temporary constructor theF.prototype =o; - return NewF (); $ } the //ECMAScript5 is a specification of prototype inheritance, which can be inherited using the Object.create () method. This function receives two parameters, a superclass object used as a subclass prototype, and the //(optional) The Property object specified for the new object, see the Code the the varperson = {//Super Class -Age:40, inRunfunction(){ theConsole.log ("Person is running"); the } About }; the varStudent = Object.create (person,{age:30}); theStudent.run ();//Person is running theStudent.age;// - + - //5. Parasitic inheritance the //using a function to implement inheritance, the function parameter is a superclass object, by copying the superclass object--Enhance the object--the way to return the result object, to implement the inheritanceBayi the functionCreateObject (obj) { the varNEWOBJ = object (obj);//Create a new object -Newobj.run =function(){//Enhanced Objects -Console.log ("Person is running"); the }; the returnNEWOBJ;//return Result Object the } the varperson ={ -Age:40, theWalk:function(){ theConsole.log ("Person is walking"); the }94 }; the varStudent =CreateObject (person); the Student.walk (); the Student.run ();98 About //Note: The incoming obj must be an instance, not a constructor. - //Cons: The method cannot be reused, as is the case with constructor functions. 101 102 103 //6. Parasitic combined inheritance104 //Avoid combined inheritance calling two-time superclass constructors to improve efficiency the functionInheritprototype (Subtype,supertype) {//inherit prototype function, parameter is subclass, superclass106 varPrototype = Object.create (Subtype.prototype);//generating a prototype copy of a superclass107Prototype.constructor = subtype;//to point the constructor of a prototype copy to a subclass108Subtype.prototype = prototype;//specifies a new prototype for the subclass, implementing the inherited property109 the }111 functionPerson () { theThi.age = 40;113 } thePerson.prototype = { theRunfunction(){ theConsole.log ("Person is walking");117 } 118 };119 functionStudent () { -Person.call ( This);121 }122Inheritprototype (Student,person);//inheriting superclass (prototype inheritance)123 varStudent =NewStudent ();124Student.run ();//"person is walking" theStudent.age;// +126 127 //The most effective way to inherit
Several modes of JavaScript inheritance