Introduction to reading and writing methods of Object property in JavaScript, javascriptproperty
In JavaScript, you can use the dot operator "." Or the brackets operator "[]" to read and write the property of an object:
Copy codeThe Code is as follows:
Var o = {x: 1, y: 2 };
Console. log (o. x); // 1
Console. log (o ["y"]); // 2
O. y = 7;
Console. log (o ["y"]); // 7
It is worth noting that if the brackets operator is used, the value type in the operator must be string or an expression that can be converted to string:
Copy codeThe Code is as follows:
Console. log (o [y]); // ReferenceError: y is not defined
Var v = "y ";
Console. log (o [v]); // 7
Unlike Java, the property of an object in JavaScript can be dynamically added or deleted. When a property value is assigned, if the property does not exist, JavaScript dynamically adds the property to the object:
Copy codeThe Code is as follows:
O. z = 99;
Console. log (o); // Object {x = 1, y = 7, z = 99}
Read the property in the prototype inheritance chain
All objects in JavaScript have a prototype and inherit property from the prototype object. Therefore, the property of an object in JavaScript is divided into two categories:
1. The object's Own property ("Own Property ").
2. property inherited from the prototype object.
When reading the property of an object, follow the following rules:
1. Search for the property to be read from the object's Own Property set ("Own property"). If you can find the property, read the property directly and return its value.
2. if the property cannot be searched from the Property set of the object itself ("Own property"), the search will continue from the prototype chain of the object, until this property is found and its value is returned.
3. If the property cannot be searched from the Property set of the object itself ("Own property"), or the property cannot be searched from all prototype objects of the object, undefined is returned.
Writing property in the prototype inheritance chain
When writing the property of a JavaScript object, the following rules are followed:
1. If the object itself has this property and this property can be written, the new value is written to this property. If the property is read-only, an error is returned.
2. If the object itself does not have this property and none of its prototype objects do, add this property to this object.
3. if the object does not own this property, but its prototype object contains this property and can be written, JS will create this property in the object; that is, this object overwrites the property in its prototype object. The property value in the prototype object remains unchanged.
4. If the object does not own this property, but its prototype object contains this property and is read-only, an error is returned.
5. If the object does not own this property, but its prototype object contains the setter method of this property, JS will call the setter method in this prototype object. It is worth noting that when you run the setter method, if variable assignment is involved, the assignment operation will be applied to the object itself, and the prototype object will not be changed. This line can be understood as: the object inherits the setter function from prototype and runs it.
From the above rules, we can find that if the property assignment operation is successful, the last modified object will always be the object itself, and its prototype object will not be changed.