1. Prototype chain inheritance
In order for subclasses to inherit the attributes of the parent class (also including methods), you first need to define a constructor. The new instance of the parent class is then assigned to the constructor's prototype.
functionParent(){THIS.name =' Mike '; }functionChild(){This.age =12; } Child.prototype =New Parent (); //child inherits the parent, through the prototype, forms the chain var test = new Child (); alert (test.age); alert (test.name) ; Get inherited attributes //Continuation prototype chain inheritance function Brother() { //brother construction this.weight = 60;} Brother.prototype = new Child (); Continue the prototype chain to inherit var brother = new brother (); alert (brother.name) ; Inherits the parent and child, pops up Mike Alert (Brother.age); //Eject 12
2. Borrowing constructors (class-type Inheritance)
function Parent(age) { this.name = [' Mike ',' Jack ',' Smith ']; this.age = age;} function child(age) {Parent.call (this,age);} var test = New Child (+); alert (test.age); alert (test.name); //mike,jack,smith Test.name.push (' Bill '); alert (test.name); Mike,jack,smith,bill
3. Combining inheritance
functionParent(age) {THIS.name = [' Mike ',' Jack ',' Smith '];this.age = Age;} Parent.prototype.run = function () {return this.name + ' is both ' + Span class= "Hljs-keyword" >this.age; }; function Child this,age); new Parent (); var test = new Child (21); Span class= "Hljs-comment" >//write new Parent (21) also line alert (Test.run ()); //mike,jack,smith is both21
4. prototype-Type Inheritance
This inheritance uses prototypes and creates new objects based on existing objects, without having to create custom types in a way called原型式继承
functionobj (o) { function f () {} F.prototype = O; return new F (); var box = {name: ' TRIGKIT4 ', arr: [ ' Baba ']}; var B1 = obj (box); alert (b1.name); //trigkit4 b1.name = ' Mike '; alert (b1.name); //mike alert (B1.arr); //brother,sister,baba B1.arr.push ( ' parents '); alert (B1.arr) ; //brother,sister,baba,parents
5. Parasitic inheritance
This inheritance is done by combining the prototype + factory model to encapsulate the process of creation.
function create(o){ var f= obj(o); f.run = function () { return this.arr;//同样,会共享引用 }; return f; }
var b2 = obj (box);
Alert (b2.name);
//TRIGKIT4 alert (B2.arr);
//brother,sister,baba,parents
Prototype inheritance first obj() creates a temporary constructor inside the function, then takes the incoming object as a prototype of the constructor, and finally returns a new instance of the temporary type.
6. Parasitic combined inheritance
functionObj(o) {functionF() {} f.prototype = O;ReturnNew F (); }functionCreate(parent,test) {var f = obj (Parent.prototype);//Create object f.constructor = Test; //Enhanced Object} function Parent this.name = name; this.arr = [ ' brother ', ' sister ', Span class= "hljs-string" > ' parents ']; } Parent.prototype.run = function () {return this.name;}; function child this,name); this.age =age;}
6 ways that JavaScript inherits