1. Declare two syntax for objects
1.1 writing
let obj = {'name':'frank'.'age':18}/ * shorthand * /
let obj = new Object{'name':'frank'}/* * */
Copy the code
1.2 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 the quotes are omitted, the key is still a string
Anyway, the key can only be a string, and even if it’s not a string, it will eventually be a string
2. How do I delete object attributes
2.1 grammar
delete obj.xxx
delete obj['xxx']
Copy the code
For example, 2.2
3. How do I view the properties of an object
3.1 Viewing Its Own Attributes
- grammar
Object.keys(obj)
Object.entries(obj)
Copy the code
- For example,
3.2 Viewing Self and Common Properties
- grammar
console.dir(obj)
Copy the code
- For example,
3.3 Two syntax for viewing attributes
- Bracketed syntax
obj['key']
Copy the code
- Some grammar
obj.key
Copy the code
- recommended
The parenthesis syntax is recommended for newcomers because the dot syntax can mislead you into thinking that a key is not a string. When you’re sure you won’t confuse the two grammars, switch to a bit of grammar
4. How do I modify or add attributes of an object
4.1 Modifying and adding its own attributes
- Direct assignment
- Batch assignment
4.2 Modifying and Adding common Attributes
- Common attributes cannot be modified or added by themselves
- You can only modify or add attributes to the prototype in the prototype
Note: Modifying a prototype can cause many problems, such as code crashes and code exceptions. If you want to change, you can only change at the beginning, and it will be very troublesome to change in between
4.3 Modifying Hidden Attributes
- Delete the prototype
obj.__proto__ = null
Copy the code
- _proto_ is not recommended
- Object. Create is recommended
5.’name’ in obj and obj.hasOwnProperty(‘name’
- ‘Name’ in obj is used to check whether the object contains an attribute name, which may be an attribute of the object itself or a common attribute of the object.
- HasOwnProperty (‘name’) is used to check whether the object contains a property name. This property name can only be its own property.