For the rewrite of prototype prototype objects, we can use the following two ways:
- Add the attributes you want to add to the prototype object using the most primitive syntax
function= "Tom"="Doctor"; Person.prototype.sayname=function() { alert (this. name);} var New Person (); Person1.sayname (); // Tom
2. Use a simple prototype syntax to encapsulate the added attributes in a literal context
function= { " Tom", "Doctor", function() { alert (this. Name); }} var New Person ();p erson2.sayname (); // Tom
Although both are available, there is a difference when you encounter a prototype rewrite.
1. The first method:
function Person () {} var New Person (); // instantiation of Person.prototype.name = "Tom"="Doctor"; Person.prototype.sayname=function() { alert (this. name); // Tom}
2. The second method:
function Person () { } varnew person (); = { "Tom" , "Doctor" ,function( { alert (this. Name); } } Friend.sayname (); // error, no Sayname method found
The principle is as follows:
At this point, the overridden prototype object disconnects the existing prototype from any previously existing object instance, and its references are considered to be original prototypes, and there is no Sayname () method in the original prototype, and the stock error.
The dynamic problem of the
JS prototype--the difference between using primitive prototype syntax and simple prototype syntax