Some recent study summary, if the error is not rigorous place, hope to point out!
Using the new operator has the following actions:
1. Create an Object temp = {}
,
2. temp.__proto__ = A.prototype
,
3. A.call(temp, arguments)
,
4. return temp
.
function A (name) { this. Name= "a"; } function B (name) { this. Name= "b"; } A.prototype.show=function() { console.log ("showaaaaa"); } B.prototype.show=function() { console.log ("showbbbbb"); }
One: Using the A = new B () method
a=New B (); Console.log (a); Console.log (a.prototype); Console.log (a.__proto__); Console.log (A.constructor);
a.show (); A.prototype.show ();
Output:
function }//Use a = new B () when the original value of a is emptied, then assign the This property and the prototype property of B to a, and the value and function are bound to the properties of a undefinedfunction }//a = new B () way A has a hidden property __proto__ points to B.prototypefunction B (name) {//with the first this. Name= "B";
Two: Using A.prototype = new B ()
a.prototype=New B (); Console.log (a); Console.log (a.prototype); Console.log (a.__proto__); Console.log (a.constructor); // a.show (); a.prototype.show (); A.show ();
Output:
functionA (name) {//Use a.prototype=new B () when a property is not changed This. Name= "A"; } b {Name:"B", Show:function}//But the prototype of a is completely covered by the This property and the prototype property of B, so the original prototype.show of A is notfunctionEmpty () {}//a has no __proto__ attribute, A.prototype has __proto__ attribute pointing to B.prototype functionFunction () {[native code]} //Overrides A.prototype so A.constructor is empty and needs to be redefined once a's constructor points to SHOWBBBBB// Same as the second uncaught typeerror:undefined is not afunctionA does not show this function, A.prototype only
Three: a = new B () and a.prototype = new B () one
a=New B (); ① A.prototype=new B (); ② Console.log (a); Console.log (a.prototype); Console.log (a.__proto__);
Console.log (a.prototype.__proto__); Console.log (a.constructor); A.prototype.show (); A.show ();
Output:
function function function}//a and A.prototype all have a__proto__ Property
function} function B (name) { this. Name= "b"; } showbbbbb //from ② showbbbbb //from ①
JavaScript about new () inherit notes