The schema of the object that was created next to the previous section:
Prototype mode:
For prototype's understanding: The function we create has a prototype (prototype) attribute, which is a pointer to an object, and the purpose of this object is to include a method based on the
The shared properties and methods for all instances. And each prototype object will automatically get a constructor (constructor) property by default. This property contains a pointer to the function where the prototype property is executed.
function person () {}person.prototype.name = ' Kim '; Person.prototype.age = 10; Person.prototype.sayName = function () { alert (this.name);}; var person1 = new Person ();p erson1.sayname (), var person2 = new Person ();p erson2.sayname ();
When we define a property in an instance that has the same name as a property already in prototype, this property masks the property in prototype. This property is not modified in prototype.
Person1.name = ' Tom ';
alert (person1.name);//tom, not Kim.
If we still ask for properties in prototype in this case, we can use the delete operator to remove this property from the instance.
The method for detecting attributes in an instance or prototype is: Person2.hasownprototype (' name ');//false returns true only if the attribute is defined in the instance.
In: operator: Regardless of whether the property is in a prototype or an instance, as long as the instance has this property, it returns true;
Alert (' name ' in Person2);//true
Person2.name = ' Jimmie ';
Alert (' name ' in Person2);//true
Delete Person2.name;
Alert (' name ' in Person2);//true
---------combine the two methods above to determine whether an attribute is in an instance or a prototype
function Hasprototypeproperty (object, name) {
return!object.hasownprototype (name) && (name in object);//true--> in the prototype;false--> in the instance
}
Object.keys (object instance), returns an enumerable property Object.getownprototypenames (Person.prototype), or the property, whether enumerable or not, will be returned. Both of these methods can be substituted
For-in to determine whether a property can be instantiated by an instance method.
Each time we use an instance to access the property, we look for the attribute in the instance and prototype, and the order of access is to find the specified property in the instance first, if it is found, it will be returned directly (so when the instance
When you define the same properties and methods as in prototype, the attributes in the instance block access to the properties in the prototype and then access the attributes in the prototype one by one (so when we are in prototype
Update or create a new property, which can be asked immediately when the instance is accessed) but when we rewrite the prototype of this instance, there are unexpected results:
function person () {}var person1 = new Person (); person.prototype={ name: ' Tom ', sayname:function () { alert (this.name); }}; Person1.sayname ();//error occurred
When we call the constructor to initialize an instance, it automatically generates a pointer ([[[Prototype]]), and the pointer points to the original prototype, and later we completely rewrite the prototype object, which is equivalent to
Cut off the connection between the constructor and the original prototype. So it's important to remember that the pointer in the instance points to the prototype object, not the constructor
The primary disadvantage of prototype mode is that all the properties and methods created are shared by all instances. So when we need some specific properties and methods that each instance has, the prototype pattern is not very appropriate.
JavaScript Advanced Programming Xi.