JavaScript in-depth prototype

Every function has a prototype property, which is a pointer to an object.

function Person(){
    
    
    Person.prototype.name = "Pinocchio";
    Person.prototype.age = 14;
    Person.prototype.getName = function(){
    
    console.log(this.name);}
}
var person1 = new Person();
var person2 = new Person();
alert(person1.getName == person2.getName);   //true

Advantage: The method will not be recreated. Disadvantages: All properties and methods are shared, and parameters cannot be initialized. For example, if there is a reference type in the prototype, then the change of one instance attribute will cause another instance attribute to also change.

6.2.4.1 Understand prototype objects

When a new function is created, a prototype property is created for the function according to specific rules, and this property points to the prototype object of the function.

By default, all automatically obtain a prototype object constructor (the constructor) attribute, the attribute contains a pointer to a function prototype property where the pointer .

Note: When looking for a prototype object, the constructor uses prototype, and the object instance uses prototype__proto__

When reading the properties of the instance, if it cannot find the properties in the prototype associated with the object, it will look for the properties in the prototype associated with the object. If it still can’t find it, it will look for the prototype of the prototype until the top level is found.

Insert picture description here

If the prototype of the instance is not found either. Since the prototype of an instance is also an object, the prototype object also has its own prototype object.

Insert picture description here

When getting person.constructor, there is actually no constructor property in person. When the constructor property cannot be read, it will be read from the prototype of person, which is Person.prototype, which happens to be in the prototype, so:

person.constructor === Person.prototype.constructor
  • Object.getPrototypeOf() It is convenient to get the prototype of an object.
alert(Object.getPrototypeOf(person1) == Person.prototype);  //true

Adding an attribute in the prototype to the instance or setting it to null will shield the attribute with the same name saved in the prototype object, and will not modify the attribute in the prototype. Unless you use delete to delete the properties in the instance, you can continue to access the properties in the prototype.

  • person1.hasOwnProperty('name') If the attribute is present in the instance, it returns true, regardless of whether it is present in the prototype.

  • "name" in person1The in operator can be used alone or in for-in. in will return true when the given property is accessible through the object, whether in the instance or prototype.

  • Object.keys(obj) Will return a string array containing all enumerable properties, excluding the prototype.

  • Object.getOwnPropertyNames() Will return all instance properties, regardless of whether it is an enumerable string array.

Guess you like

Origin blog.csdn.net/Pinoochio/article/details/113706682