A brief talk on JavaScript overlay prototype and change prototype _javascript skill

Source: Internet
Author: User

Overwrite prototype

Prisoner example//1. Defines the prototype object var proto = {Sentence:4,//imprisonment years Probation:2//probation years}; 
 2. Define the constructor of the prototype object var prisoner = function (name, id) {this.name = name; 
This.id = ID; 
}; 
3. Associating the constructor with the prototype Prisoner.prototype = Proto; 4. Instantiate object--using factory function to instantiate object var Makeprisoner = function (name, ID) {//Use factory function to strength object prisoner var prisoner = object.create (pr 
 OTO); 
 Prisoner.name = name; 
 Prisoner.id = ID; 
return prisoner; 
 
}; 
 
var firstprisoner = Makeprisoner (' Joe ', ' 12A '); Firstprisoner.sentence cannot find the sentence property in the Firstprisoner object,//So look up the prototype of the object and find both of these output 4 Console.log ( 
Firstprisoner.sentence); 
Console.log (firstprisoner.__proto__.sentence); 
Set the object's sentence property to ten firstprisoner.sentence = 10; 
Outputs 10//determines that the property value on the object has been set to Console.log (Firstprisoner.sentence); 
But the object's prototype did not change, and the value was still 4 console.log (firstprisoner.__proto__.sentence); 
Removes the delete firstprisoner.sentence from the object in order to get the property back to the value of the prototype; Next, the JavaScript engine cannot find the attribute on the object,//Must go back and look up the prototype chain and find the attribute on the prototype object//Both of thePut 4 console.log (firstprisoner.sentence); Console.log (firstprisoner.__proto__.sentence);

Ubuntu Terminal node output

xxh@xxh-e440:~/workspace$ node T6 
4 
4 
4 
4 
4 

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

This article on the JavaScript cover prototype and change the prototype is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.