Constructor
Class declaration
1.function statement
function Animal(name){
this.name = name
}
2.class statement
class Animal{ constructor(name){ this.name = name } }
Class instantiation
Examples of the new operator
let animal = new Animal('pig')
Class inheritance
// inheritance by the constructor function Parent1 () { the this .name = 'parent1' } function Child1 () { Parent1.call ( the this ); the this .Type = 'the child1' } // insufficient, the prototype can not be fully inherited Parent1 properties and methods
// defined, for example Parent1.prototype.pName = 'pName' Child1 can not inherit
// means of the prototype implementation inheritance chain function parent2 () { the this .name = 'parent2,' the this .arr = [l, 2,3 ] }; function Child2 () { the this .Type = 'child2' } Child2.prototype = new new parent2 (); // insufficient, if there is an object in the constructor, then modify the parameters of an instance of the object will affect another example, // since s1 .__ proto__ === s2 .__ proto__ a common prototype object, which is arr is a reference type value S1 = new new Child2 (); S2 = new new Child2 (); s1.arr.push ( . 4 ) the console.log (s1.arr, s2.arr) // (. 4) [. 1, 2, 3, 4] (4) [1, 2, 3, 4]
// combination. 1 function Parent31 () { the this .name = 'P31' ; the this .arr = [l, 2,3 ] } function Child31 () { Parent31.call ( the this ); // Get the attribute structure the method and the this .Type = 'C31' ; } Child31.prototype = new new Parent31 (); // get property methods and prototype var S3 = new new Child31 (); var S4 = new new Child31 (); s3.arr.push ( . 4 ) the console.log (s3.arr, s4.arr) // [1,2,3,4] [l, 2,3] // combinatorial optimization 2, reducing the number of runs Parent31 constructor function Parent33 () { the this .name = 'P33' } function Child33 () { Parent33.call ( the this ); // Get structure attributes and methods of the this .Type = 'C33' ; } var Test = new new Child33 (); Child33.prototype = Parent33.prototype; // get property methods and prototypes // because prototype inheritance of two or more of them is a result in the same constructor the console.log (Test the instanceof Child33, Test the instanceof Parent33) // to true to true // combinatorial optimization 3 function Parent5 () { the this .name = 'P5' } function Child5 () { Parent5.call ( the this ); // get property methods and structure of the this .Type = 'C5' ; } var test2 = new new Child5 ( ); Child5.prototype ; = the Object.create (Parent5.prototype)
Child5.prototype.constructor = child5
//object.create method creates a new object, an existing object to provide the use of the newly created object of __proto__ on obtaining prototypes properties and methods, change the constructor console.log (test2 instanceof Child5, test2 instanceof Parent5) // to true false
// ES6 class inheritance in the extends class A { constructor () { the this .name = 'A' } say () { the console.log ( 'say' ) } }
// use extends B A succession of the problems exemplified instanceof the extends B {A class constructor () { Super (); // corresponds A.prototype.constructor.call (the this) the this .Type = 'B' } }