A, prototype inheritance chain [ prototype object constructor is sub-instance of the parent constructor ] [ inherit properties and methods of the prototype ]
1, each constructor [ prototype ] has a prototype object, the prototype object contains a pointer [constructor pointer constructor ], and the solid
embodiment contains an internal pointer to the prototype object [ __proto__ ]. When the prototype object is equal to a further instance of the type that is inherited.
eg:
// Create the parent class Animal constructor function Animal (name, Age) { the this .name = name; the this .age = Age; } // add sayName () methods to the prototype object of Animal Animal.prototype.sayName = function () { the console.log ( the this .name); } // subclass Dog Dog.prototype] [prototype object pointer pointing to an object instance Animal Dog.prototype = new new Animal (); // a pointer to a constructor Dog Dog type = Dog.prototype.constructor Dog; Dog.prototype.sayAge = function () { the console.log (the this .age); } // Create instance of the object class Dog dog var dog = new new Dog ( 'Cai', 2, 'Yellow' ); dog.sayName (); // Cai [method] can call the parent class dog .sayAge (); // 2 [can call the method itself prototype object]
: 2, carefully defined method
[a method of super-type subtype coverage or need does not exist in the superclass method of adding, after all the code will need to add method prototypes in succession after that is to replace the prototype of the statement ]
3, the prototype chain related issues:
a, is achieved when inherited through the prototype, the prototype will actually become an example of another type, the original instance properties will now become a prototype attribute
b, when you create instances of subtype , can not pass parameters to the constructor [supertype pseudo-class inheritance back to solve ]. Therefore rarely used alone in practice the prototype chain
two classic inheritance (pseudo-inherited) [ Inheritance instance attribute ]
1, using the sub-class constructor call () or apply () method of changing the method object is called, subclass constructor used to calling the parent class constructor call () or apply () and
which call the object to subclass object
eg:
// Create the parent class Animal constructor function Animal (name, Age) { the this .name = name; the this .age = Age; } // add sayName () methods to the prototype object of Animal Animal.prototype.sayName = function () { the console.log ( the this .name); } // Create a subclass of Dog constructor function Dog (name, Age, Color) { // use the call function specified object configured to call a function to process Animal Dog Animal.call ( the this , name, Age); the this .color = Color; } // create a class instance object Dog Dog var= Dog new new Dog ( 'Cai', 2, 'Yellow' ); the console.log (dog.name); // Cai
Third, the combination of inheritance (prototype inheritance chain [chain inheritance and composition pseudo classical inherited ]) [one kind often used inheritance]
1, the principle is: a prototype of the prototype implementation inheritance chain properties and methods, and constructors realized by borrowing examples of the inheritance of property
eg:
// Create the parent class Animal constructor function Animal (name, Age) { the this .name = name; the this .age = Age; } // add sayName () methods to the prototype object of Animal Animal.prototype.sayName = function () { the console.log ( the this .name); } // create an object instance Animal Animal var Animal = new new Animal ( 'white',. 1 ); animal.sayName (); the console.log (Animal); // prototypes inheritance chain, the general method for the parent class inherited // subclass Dog Dog.prototype] [prototype object pointer pointing to an object instance Animal Dog.prototype = new newAnimal (); // a pointer to a constructor Dog Dog constructor Dog.prototype.constructor = Dog; Dog.prototype.sayAge = function () { the console.log ( the this .age); } // Create a subclass of Dog configured function function Dog (name, Age, Color) { / * this.name = name; this.age = Age; * / // classic inheritance (pseudo inheritance), generally inherit properties Animal.call ( the this , name, Age); the this .color = Color; } // create an object class instance Dog Dog var Dog = new new Dog ( 'Cai', 2, 'yellow'); Dog.sayName (); // can call the parent class method dog.sayAge (); // can call itself has front Dog [prototype prototype object points to a new Animal () method of the object instance in the object] // animal.sayAge (); // object's parent class method can not be used to create a subclass prototype object not iS a function animal.sayAge // Array inherited from who console.log (Array.prototype .__ proto __ constructor.); / / Object
Related legend is as follows: