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

  1. Key names are strings, not identifiers, and can contain any character
  2. Quotes can be omitted, and then only identifiers can be written
  3. 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

  1. grammar
Object.keys(obj)
Object.entries(obj)
Copy the code
  1. For example,

3.2 Viewing Self and Common Properties

  1. grammar
console.dir(obj)
Copy the code
  1. For example,

3.3 Two syntax for viewing attributes

  1. Bracketed syntax
obj['key']
Copy the code
  1. Some grammar
obj.key
Copy the code
  1. 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

  1. Direct assignment

  1. Batch assignment

4.2 Modifying and Adding common Attributes

  1. Common attributes cannot be modified or added by themselves

  1. 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

  1. Delete the prototype
obj.__proto__ = null
Copy the code
  1. _proto_ is not recommended

  1. Object. Create is recommended

5.’name’ in obj and obj.hasOwnProperty(‘name’

  1. ‘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.

  1. HasOwnProperty (‘name’) is used to check whether the object contains a property name. This property name can only be its own property.