Understand the dynamics of JavaScript prototypes

Source: Internet
Author: User
Tags function prototype

function person () {}person.prototype.name =   ' Sun '; Var person1 = new person (); Alert ("Person original prototype:" +person1.name);// The first rewrite of the prototype person.prototype = {constructor : person,   //an explicit declaration constructorname  :  ' Cheng ', Age : 18};alert ("prototype is overwritten after P1:" +person1.name);    //sun//new instance var  Person2 = new person (); alert ("P2 after prototype coverage:" +person2.name)    //chengalert ("original    : "+ person1.constructor);    //points to Personalert (" after: "+ person2.constructor);    //pointing person//The second time covering prototype person.prototype = {constructor : person,name :  ' FFFF ', Age : 18};alert ("prototype covered after P1:" +person1.name);    //sunalert ("Prototype covered P2:" +person2.name) ;    //chengvar person3 = new person (); alert ("P3 after prototype coverage:" +person3.name);    //FFF 

The above tests can be seen:

1: The object instance created before the prototype rewrite, always points to the pre-rewrite prototype

2: An object instance created after the prototype rewrite, always pointing to the newly rewritten prototype;

3: Multiple rewritten prototypes can exist at the same time and can all be associated with a function object through constructor. ?

In summary, the [[prototype]] pointer in the object instance always points to the most recent function prototype before the object instance is created and never changes;

Multiple prototypes can exist at the same time and are associated with function objects; (you need to explicitly declare constructor)


Understand the dynamics of JavaScript prototypes

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.