JavaScript covers prototypes and changes prototypes

Source: Internet
Author: User

Overwrite prototypes

Prisoner example//1. Defining a Prototype object var proto = {Sentence:4,//imprisonment Probation:2//probation period};//2. Defines the constructor of the prototype object var prisoner = function (name, ID  ) {this.name = name; This.id = id;};/ /3. Associating constructors to prototypes Prisoner.prototype = Proto;//4. Instantiating an object-using a factory function to instantiate an object var makeprisoner = function (name, ID) {//  Prisoner var prisoner = Object.create (proto) by using the factory function as the strength of the object;  Prisoner.name = name;  Prisoner.id = ID; return prisoner;}; var firstprisoner = Makeprisoner (' Joe ', ' 12A ');//firstprisoner.sentence cannot find sentence attribute in Firstprisoner object,// So find the prototype of the object and find the both of these output 4console.log (firstprisoner.sentence); Console.log (firstprisoner.__proto__. sentence);//Set the object's sentence property to 10firstprisoner.sentence = 10;//outputs 10//To determine that the property value on the object has been set to 10console.log ( firstprisoner.sentence);//But the prototype of the object does not change, the value is still 4console.log (firstprisoner.__proto__.sentence);//In order to get the property returned to the value of the prototype, Remove a property from an object delete firstprisoner.sentence;//Next, the JavaScript engine can no longer find the property on the object,//Must go back to find the prototype chain and find the attribute on the prototype object//Both of these Output 4console.log (firstprisoner.sentence); Console.log (Firstprisoner. __proto__.sentence); 

Ubuntu Terminal node output

[Email protected]:~/workspace$ node t64410444

So what happens if you change the property value of the prototype object? I know you're thinking.

See next section




JavaScript covers prototypes and changes 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.