A, definitions,
- Describe the characteristics of an object using key:value pairs (each object is a complex, with zero to multiple sets of key:value pairs)
- { key : value , … } Each group of key-value pairs is in the format of key: value, separated by commas
- Key cannot be a reference data type, and value can be any data type
Two syntax for declaring objects
Ex. :
Let person2 = new Object({'name':'小明'}Copy the code
Details:
- Key names are strings, not identifiers, and can contain any character
- Quotes can be omitted, and then only identifiers can be written (even if omitted, the key is still a string)
Add, delete, change and check key value pairs
3.1 Query/Obtain
- Get the value
1, object. Property name
Based on this method, the property name is. The latter way, attribute names cannot be numbers
2. Object [attribute name]
To operate in this way, you need to ensure that the property name is a value (string/number/Boolean). If it is not a value but a variable, it will operate on the value stored in the variable as the property name of the object. If the property name is a number, you can only use this method
If the specified attribute does not exist, the obtained attribute value is undefined (no error)
3.2 delete
- True delete: Removes the property from the object completely
delete obj.name
- Fake delete: The current property still exists, but the property value is empty
obj.name = null
3.3 increase | change
- A single assignment
Object property names (keys) are not allowed to be duplicated
This property is new if it did not exist before
If the name of the property is present, the value of the property is changed
Ex. :
let obj = {
sex: 0
};
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = =
obj.name = 'Joe'; / / = > new
obj['name'] = "Bill"; //=> modify Because name: 'Zhang SAN' already exists in obj, so this operation is modified
Copy the code
- Batch assignment
Use the Object. The assign ()
Ex. :
Object.assign(obj,{age:18.gender:'man'})
Copy the code
- Modifies the hidden properties of an object
We all know that every object has a hidden attribute __proto__ that points to a prototype, or a common attribute
To modify it, use Object.create
Ex. :
let zhou = {'countries':'China'}
let obj = Object.create(zhou)
Copy the code
This means that when creating an obj object, the specified prototype is Zhou. The Zhou object also has a hidden attribute __proto__ that points to the root prototype, which makes up the prototype chain.
The appendix
‘Property name’ the difference between an object and an object. HasOwnProperty (‘ property name ‘)
'toString' in obj / / return trueThere is no distinction between private and public propertiesCopy the code
onj.hasOwnProperty('toString') / / returns falseDistinguish between private and public properties. Only private properties are returnedtrue
Copy the code