1. Prototype chain inheritance
Subclasses use base class objects to override prototype
functionsupertype () { This. property =true; } SuperType.prototype.getSuperValue=function(){ return This. property; }; functionsubtype () { This. Subproperty =false; } //inherit from Supertype Subtype.prototype = new supertype (); //New MethodSubType.prototype.getSubValue =function (){ return This. Subproperty; }; //Override existing MethodSubType.prototype.getSuperValue =function (){ return false; }; varInstance =Newsubtype (); Alert (Instance.getsupervalue ()); //false
Main disadvantage: When inheriting from a prototype, the prototype becomes an instance of another type, so the original instance attribute is transformed into a prototype attribute in the subclass.
2. Borrowing constructors
Use call/apply to call base class constructor methods in subclasses
functionsupertype (name) { This. Name =name;}functionsubtype () {//inherit from supertype passing to an argumentSupertype.call (This, "Nicholas"); //Instance Property This. Age = 29;}varInstance =Newsubtype (); alert (instance.name); //"Nicholas";alert (instance.age);// in
Cons: Not true inheritance, cannot execute a method of a prototype in a base class.
3. Combination Inheritance (pseudo classic inheritance)
The most common way to inherit is to perform both the borrowed constructor and the prototype chain pass.
The biggest problem is that the two-time super-type constructor is called in any case.
functionsupertype (name) { This. Name =name; This. colors = ["Red", "Blue", "green"];} SuperType.prototype.sayName=function() {alert ( This. name);};functionsubtype (name, age) { Supertype.call ( This , name); This. Age =Age ;} Subtype.prototype = new supertype (); SubType.prototype.sayAge=function() {alert ( This. age);};varInstance1 =NewSubtype ("Nicholas", 29); Instance1.colors.push ("Black"); alert (instance1.colors); //"Red,blue,green,black"Instance1.sayname ();//"Nicholas";Instance1.sayage ();// in
4. prototype-Type Inheritance
The idea is to borrow prototypes to create new objects based on existing objects.
function object (o) { function F () {} F.prototype = o; return new F (); varperson ={name:"Nicholas", friends: ["Shelby", "Court", "Van"]}; var anotherperson = object (person); /* Note that the new Object.create () method in ES5 has standardized the prototype inheritance, which takes two parameters, one for the new object prototype, and one additional method for the new object, var Anotherperson = Object.create ( person); */Anotherperson.name= "Greg"; AnotherPerson.friends.push ("Rob.");varYetanotherperson =object (person); Yetanotherperson.name= "Linda"; YetAnotherPerson.friends.push ("Barbie"); alert (person.friends); //"Shelby,court,van,rob,barbie"modification of an object in a subclass affects the invocation of the base class's object.
5. Parasitic combined inheritance
It is mainly used to solve the problem of calling two constructors in combinatorial inheritance, which inherits the property by borrowing the constructor, and inherits the method through the compositing form of the prototype chain.
Its high efficiency is reflected in it only calls a supertype constructor, avoids the creation of unnecessary, superfluous properties on the prototype of subtype, the most ideal way to inherit.
functionObject (o) {functionF () {} F.prototype=o; return NewF ();} function inheritprototype (subtype, supertype) { var prototype = object ( Supertype.prototype); // Create object prototype.constructor = subtype; // Augment object Subtype.prototype = prototype; // Assign Object } functionsupertype (name) { This. Name =name; This. colors = ["Red", "Blue", "green"];} SuperType.prototype.sayName=function() {alert ( This. name);};functionsubtype (name, age) { Supertype.call ( This , name); This. Age =Age ;} Inheritprototype (subtype, supertype); SubType.prototype.sayAge=function() {alert ( This. age);};varInstance1 =NewSubtype ("Nicholas", 29); Instance1.colors.push ("Black"); alert (instance1.colors); //"Red,blue,green,black"Instance1.sayname ();//"Nicholas";Instance1.sayage ();// in
The primary method inherited in JavaScript