1. Declare two syntax for objects

let obj = { 'name':'frank','age': 18 }
let obj = new Object({'name': 'frank'})
Copy the code

2. How do I delete object attributes

delete obj.xxx 
delete obj['xxx']
Copy the code

Contains no attribute name

'xxx' in obj.xxx === undefined
Copy the code

Contains the attribute name, but the value is undefined

'xxx' in obj && obj.xxx === undefined

Copy the code

3. View object properties

  • View all of its properties
Object.keys(obj)
Copy the code
  • View self + Common properties
console.dir(obj)

Copy the code

Or print obj.proto in sequence with Object.keys

  • A property is judged to be self – owned or common
obj.hasOwnProperty("xxx")
Copy the code

4. How do I modify or add attributes of an object

  • Direct assignment
Obj ['name'] ='frank' obj['na'+'me']='frank' obj['na'+'me']='frank'Copy the code
  • Batch assignment
Object.assign(obj, {age: 18, gender:'man'})
Copy the code

5. How do I modify or add attributes of an object

  • Common attributes cannot be modified or added by themselves
Let obj ={},obj2={} // toString obj. ToString = 'XXX' will only change obj's own property obj2. ToString is still on the prototypeCopy the code
  • Hard modify or add attributes to the prototype
Obj. __proto__. ToString = 'XXX' / / do not recommend the application Object. The prototype. ToString = 'XXX'Copy the code

In general, don’t modify a prototype, it can cause a lot of problems

  • Modifying hidden attributes
let obj = {name: 'frank'}
let common = {kind:'human'}
Copy the code
  • Object. Create is recommended
let obj = Object.create(common)
obj.name = 'frank'
Copy the code

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

Object obj contains its own attribute ‘name’