First, make sure that the instance cannot access the attributes and methods in the prototype object, not only because the prototype object is rewritten to cut off the relationship between the constructor and the original prototype. In JavaScript Zhongyuan and objects, I have mentioned how instances can access the attributes and methods of rewritten prototype objects.
Function person () {} var person = new person (); person. prototype = {// constructor: person, name: 'zxs ', age: 24, sayname: function () {alert (this. name)} person. sayname ();
The prototype of the constructor is empty when the above Code instantiates an object. It does not have any attributes other than the default attributes. The prototype of the rewrite constructor does cut off the relationship between the constructor and the original prototype.
Attributes and methods in the prototype object of the constructor after the new operator are added to the person object. Because the method above adds new attributes and methods for the function prototype is not dynamic, the person cannot access the newly added attributes and methods.
After rewriting the prototype object, it is like the following code:
VaR o = {Name: 'zxs '} var OBJ = O; O = {} console. Log (O. Name );
The output value is undefined, because the object is a reference type, "=" is a value assignment operator, and its operation order is from right to left. O ={} indicates that the direction of O has changed and is an empty object.
Person. prototype. mothed = function () {} and person. prototype = {mothed: function () {} is similar to ARR = [] and ARR. like push (), the former changes itself, and the latter completely changes itself.