Two syntax for declaring objects

  • Objects are unordered collections of data
  • Is a key/value pairkey:valueA collection of

The statement syntax

1 / / grammar
let obj = {'name':'jack'.'age':18}
// Name is the key name, jack is the key value, age is the key name, 18 is the key value

2 / / grammar
let obj = new Object({'name':'jack'.'age':18})

Copy the code

Small details 🧐 :

  1. The key name is a string, not an identifier, and can contain any character
  2. The quotation marks for key names can be omitted, but after that they can only be identifiers
  3. Omit the quotes, and the key name is still a string
  4. eachkeyThese are the property names of the object
  5. eachvalueAre the property values of the object

Variable attribute name

Implementation using []

let slot = 'name'
let obj ={[slot]:'jack' } // The attribute name is name

/ / is equivalent to
let obj = {'name':'jack'}

// Not equivalent to
let obj = {slot:'jack'} attribute is named slotCopy the code

How do I delete an object’s properties

1 / / method
delete obj.xxx 

2 / / method
delete obj['xxx']

3 / / method
let d_key = 'ket_name'
delete obj[d_key]   
// Note: This method uses the form of a variable to correspond to the object's property name
Copy the code

How do I view object properties

// View all of its properties
Object.keys(obj)

// View itself and common properties
console.dir(obj)
/ / or
Object.keys(obj)
Object.keys(obj.__proto__)

// Check whether the property is owned
'xxx' in obj  === true/false

// Check whether the properties are self-contained or shared
obj.hasOwnProperty('xxx')
Copy the code

Difference between in and hasOwnProperty

In determines all the properties of an object, including the properties of the object itself and its prototype.

HasOwnProperty determines whether the object itself has a property.

So when hasOwnProperty is used in combination with in, it is possible to determine whether a property is a stereotype property.

How do I add a property or modify the property value of an object

let obj = {name: 'jack'}

// Add attributes
obj.age  // Do not assign directly, return undefined on read
obj.gender = 'male' // Direct assignment

obj['height'] = '168cm' 
/ / equal to
obj.height='168cm'

// Add one by one more troublesome, batch assignment to help you
Object.assign( obj, {age,gender:'male'.height:'168cm'})

// Modify the attribute value
obj.gender = 'female'
obj['height'] = '200cm' 

// Modify the shared attribute valueObj.__proto__. Common attribute name ='value'   // Not recommended
/ / equal to
Object.prototype. Common property name ='value'
Copy the code

How do I add common properties

Object. Create (target_obj)

let obj = {}
let common = {kind:'human'}
let obj = Object.create(common)
obj.name = 'jack'
Copy the code

Note that 🧐 : object.create () needs to be used on empty objects, otherwise existing properties will be overwritten

The last pit, have not found a good answer, how to modify the property name? Later have time to study.

A Word of the day: Keep breaking, not repeating.