In JavaScript, you can read and write the object's property through the dot operator "." or the bracket operator "[]":
The code copy is as follows:
var o = {x:1, y:2};
console.log(ox);//1
console.log(o["y"]);//2
oy = 7;
console.log(o["y"]);//7
It is worth noting that if the bracket operator is used, the value type in the operator must be string, or an expression that can be converted into a string:
The code copy is as follows:
console.log(o[y]);//ReferenceError: y is not defined
var v = "y";
console.log(o[v]);//7
Unlike Java language, properties of objects in JavaScript can be added or deleted dynamically. When assigning a property, if the property does not exist, JavaScript will dynamically add this property to the object:
The code copy is as follows:
oz = 99;
console.log(o);//Object {x=1, y=7, z=99}
Reading of property in the prototype inheritance chain
All objects in JavaScript have a prototype prototype object and inherit property from the prototype object; therefore, the properties of an object in JS are divided into two categories:
1. The property owned by the object itself ("Own Property").
2. Property inherited from the prototype object.
When reading the property of an object, the rules followed are as follows:
1. Search for the property that needs to be read from the object's own property collection ("Own Property"); if it can be searched, it will directly read the property and return its value.
2. If the property cannot be searched from the object's own property collection ("Own Property"), then continue to search from the object's prototype prototype chain until the property is searched and its value is returned.
3. If the property cannot be searched for in the object's own property collection ("Own Property") and the property cannot be searched for in all the object's prototype objects, then undefined is returned.
Write property in the prototype inheritance chain
When writing to the property of a JavaScript object, the rules followed are as follows:
1. If the object itself has the property and the property is writable, write the new value to the property. If the property is read-only, an error is reported.
2. If the object itself does not have the property and does not exist in all its prototype objects, add this property to the object.
3. If the object itself does not have the property, but the property exists in its prototype object and is writable, then JS will create this property in the object; that is, the object overrides the property in its prototype object. This property value in the prototype object remains unchanged.
4. If the object itself does not have the property, but the property exists in its prototype object and is read-only, an error will be reported.
5. If the object itself does not have the property, but the setter method of the property exists in its prototype object, then JS will call the setter method in the prototype object. It is worth noting that when running the setter method, if variable assignment is involved, the assignment operation will act on the object itself, and the prototype object will not be changed. For this behavior, it can be understood as: the object inherits the setter function from the prototype and executes it.
From the above rules, we can find that if the assignment operation to the property is successful, then the object itself will always be modified in the end, and its prototype prototype object will not be changed.