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