Object properties, prototypes, and prototype chains
hahaha, my second blog, is about object properties, prototypes, and prototype chains. Maybe these are just some small dots in series, logic is not very strong. Therefore, the readability and understanding of the article will bring some trouble. However, today I have moved forward a small step, for the subsequent study and laid a lost foundation, although not particularly good understanding, but I will understand the second, the third time. Well. Come on!
Object
The object contains a series of properties. These properties are unordered, and each property has a string key and the corresponding value.
The simplest way to create a custom object is to create an object instance and then add properties and methods to it.
Property Type: Data properties and access properties.
Data properties:
Configurable: Indicates whether the property can be redefined by deleting the property, whether the attribute is modified, and so on. The default value of this property is true.
Enumerable: Indicates whether the attribute can be returned through the for-in loop. The default value of this property is true.
Writable: Indicates whether the value of the property can be modified. The default value of this property is true.
Value: The data value that contains this property. When reading a property value, the new value is saved in this position when it is read at this location and the property value is written. The default value for this feature is undefined.
To modify the default properties, you must use the Object.defineproperty () method.
The Object.defineproperty () method can be called multiple times to modify the same property, but after setting the configurable attribute to false there is a limit.
Accessor properties:
Accessor properties do not contain data values, they contain a pair of getter and setter functions (not required). The Getter function is responsible for returning valid values, and the setter function is responsible for handling the data.
Accessor properties There are four attributes, except configurable and enumerable, and get and set methods, get is called when the property is read, set is called when the property is written. And the default value is undefined.
Configurable and enumerable cannot be modified in browsers that do not support the Object.defineproperty () method.
"Prototype and prototype chain"
Prototype mode: Each function we create has a prototype (prototype) attribute, which is a pointer to an object that contains properties and methods that can be shared by all instances of a particular type, whereas a prototype object allows all object instances to share the properties and methods it contains. Literally, prototype is the prototype object that creates the object instance by invoking the constructor.
Prototype object: Whenever a new function is created, a prototype property is created for the function according to a specific set of rules, which points to the prototype object. By default, all prototype objects automatically get a constructor (constructor) property that contains a pointer to the function where the prototype property is located.
Use the hasOwnProperty () method to detect whether a property exists in an instance or exists in a prototype. The prototype returns false, and the instance returns True.
Prototypes and in operators: The in operator can be used alone or in a for-in loop, and both the in operator and hasOwnProperty () can be used to determine whether a property exists in an object or a prototype. Such as:
function person () {}
Person.prototype.name= "Smile";
person.prototype.age=19;
Person.prototype.sayname=function () {
alert (this.name);
};
var person1=new preson ();
Alert (Person1.hasownproperty ("name"));//false
Alert ("name" in Person1); True
As long as the in operator returns True,hasownproperty () returns FALSE, you can determine that the property is a property in the prototype.
Prototype chain: The prototype chain is the main way to implement inheritance. The basic idea is to use a prototype to let a reference type inherit another reference type's method. Relationships to constructors, prototypes, and instances: each constructor has a prototype object, and the prototype object includes a pointer to the constructor, and the instance contains a pointer to the inside of the object. If we let the prototype object be equal to another instance of the type, then the prototype object contains a pointer to another prototype, and another prototype contains a pointer to another constructor. If another prototype is another type of example, such a layer of progressive, it constitutes an example and prototype chain. This is the basic concept of the prototype chain. Also, the end of the prototype chain must be null.
In the case of inheritance through the prototype chain, the search process must continue up along the prototype chain.
All reference types inherit object by default, and this inheritance is implemented through the prototype chain. The default prototype for all functions is an instance of object, so the default prototype will contain an internal pointer to Object.prototype.
Problem with the prototype chain:
The prototype property in the prototype chain that contains the reference type value is shared by all instances, which is why the attribute is defined in the constructor, not in the prototype function. When you implement inheritance through a prototype, the prototype actually becomes an instance of another type. As a result, the original attribute has naturally become the prototype attribute of the present.
When you create an instance of a subtype, you cannot pass parameters to a super-type constructor.
JavaScript (2)--object properties, prototypes, and prototype chains