JavaScript inheritance is a bad topic, I'll just talk about it.
First, the replication inheritance of JavaScript
The inheritance of JavaScript has replication inheritance and prototype inheritance, not too much for replication inheritance, and cannot be verified by instanceof
// copy inheritance, Prototype.js's extend=> function Extend (destination,source) { for (var in source) destination[ Property]=Source[properyt]; return destination;}
Second, JavaScript prototype inheritance
JS prototype inheritance is based on the prototype chain lookup, JS each function has the prototype property and the __proto__ property, each instance of the __proto__ property points to the prototype of the function (Es6 in the instance of __proto__ point to this function), The following example proves this point.
function A () {Console.log ( this . __proto__.aa); // 1 }a.prototype ={aa: 1} var a=new A;console.log (A.AA); // 2 a.__proto__={aa: 3} delete A.aa; // Console.log (A.AA); // 3
The instance finds the method by the prototype chain, finds its own property, does not go to the prototype of the constructor function, and finds it in the prototype of the constructor function, only to the prototype of function. Then we let A's prototype equal to an instance of a, do not complete the inheritance.
function A () {}a.prototype={ aa:1}function Bridge () {};bridge.prototype= A.prototype; function B () {}b.prototype=new Bridge (); B.prototype.constructor=b;
This inheritance is done with a bridge function, because when a has a lot of content, the instantiation of a consumes more, and there is no use, just use an empty function to do the bridge. Here we go. The constructor of the instance points to itself, which completes the inheritance.
var b=New B; b.prototype.cc=function() { alert (3)}console.log (b.__proto__==b.prototype); trueconsole.log (B.__proto__.__proto__===a.prototype); // true
The __proto__ of B here is the prototype that points to the constructor function.
Chat JavaScript inheritance and prototyping