In JavaScript, you can use the dot operator "." or the bracket operator "[]" to read and write the property of an object:
Copy Code code 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 important to note that if you use the bracket operator, the value type within the operator must be string, or an expression that can be converted to string:
Copy Code code as follows:
Console.log (O[y]);//referenceerror:y is not defined
var v = "Y";
Console.log (O[v]);//7
Unlike the Java language, the property of an object in JavaScript can be added or deleted dynamically. When an assignment is made to a property, JavaScript dynamically adds the property in the object if the property does not exist:
Copy Code code as follows:
O.Z = 99;
Console.log (o);//object {x=1, y=7, z=99}
The reading of the property in the prototype inheritance chain
All objects in JavaScript have a prototype prototype object and inherit the property from the prototype object; Therefore, the property of an object in JS is divided into two main categories:
1. The property owned by the object itself ("Own property").
2. Property inherited from a prototype object.
When you read the property of an object, the following rules are followed:
1. Searches for the property that needs to be read from the property collection ("Own property") of the object itself, and if it can be searched, reads the property directly and returns its value.
2. If the property cannot be searched from the object's own property collection ("Own property"), the search continues from the prototype prototype chain of the object until the property is searched and its value is returned.
3. If you cannot search for the property from the property collection ("Own property") of the object itself, and you cannot search for the property from all prototype objects in the object, return undefined.
The writing of the property in the prototype inheritance chain
When writing to the property of a JavaScript object, the following rules are followed:
1. If the object itself has the property, and the property is writable, the new value is written to the property. If the property is read-only, an error occurs.
2. If the object itself does not have the property, and the property is not present in all of its prototype objects, this property is added to the object.
3. If the object itself does not have this property, but its prototype object exists in the property and can be written, then JS will create the property in the object, that is, the object overwrite the property in its prototype object. The property value in the prototype object is unchanged.
4. If the object itself does not have the property, but its prototype object exists in the property and is read-only, an error is provided.
5. If the object itself does not have this property, but its prototype object has a setter method for that property, then JS invokes the setter method in the prototype object. It is important to note that when you run the setter method, if the variable assignment is involved, the assignment action will work on the object itself, and the prototype object will not change. For this behavior, it can be understood that the object inherits the setter function from the prototype and executes it.
As can be seen from the above rules, if the property assignment operation succeeds, then the object itself is always modified, and its prototype prototype object is not changed.