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.