Two syntax for declaring objects
Object definition
- Unordered collection of data
- A collection of key-value pairs
writing
- 1
let obj = {'name': 'frank', 'age':18}
- Let Object name =new Object({‘ keyname ‘:’ keyvalue ‘})
let obj = new Object({'name': 'frank'})
Pay attention to
- Key names are strings, not identifiers
- Quotes can be omitted, and even if they are omitted, the key is still a string
Object.keys(obj) can obtain all the key names (attribute names) of an Object named obj!
How do I delete an object’s attributes
- 1.delete obj.xxx
- 2.delete obj[‘xxx’]
Either way, you can remove the property named XXX from the object obj.
Check whether the object has an attribute name:
'xxx' in obj === ture
'xxx' in obj === false
Copy the code
XXX is the name of the attribute to be judged, and obj is the name of the object. Returns true for presence, false for absence.
Check if attribute name exists, but attribute value is undefined:
'xxx' in obj && obj.xxx === undefined
Copy the code
XXX is the name of the attribute to be judged, and obj is the name of the object. Return undefined if the property value is undefined, false otherwise.
How do I view the properties of an object
- View all of your properties:
Object.keys(obj)
- View self + Common properties:
console.dir(obj)
Or print obJ._ _ proto_ _ with object. keys - How do you determine if a property is self or common
obj.hasOwnProperty('XXX')
- View a property:
obj['name']
orobj.name
How do I modify or add attributes to an object
Direct assignment
let obj={'name':'frank'}
obj.name='frank'
Where name is a stringobj['name']='frank'
obj['na'+'me']='frank'
let key='name'; obj[key]='frank'
- Batch assignment can be used
Object.assign(obj,{'age':18,'gender':'man'})
, where obj is the object name, age key name, and 18 key value - Modifying common Attributes
Object.prototype['toString']='xxx'
- Modifying hidden attributes
let obj = Object.create(common)
Obj. hasOwnProperty(‘name’)
'name' in obj
Check to see if the property name is in OBj, but it can’t tell whether the property is unique to itself or common.obj.hasOwnProperty('name')
Used to check whether name is a unique or common property of obj, returning true if it is unique and false if it is common