JavaScript about new () inherit notes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.