Conceptual understanding
Object definition:
- Unordered collection of data
- A collection of key-value pairs
Writing:
let {obj = 'name', 'bubu_sourire', 'age': 18 } let obj = new Object ({ 'name', 'bubu_sourire' }) console.log ({ 'name', 'bubu_sourire', 'age' : 18})
note:
- Keys are strings, not the identifier may comprise any character key
- Key quotes can be omitted, can only be written after the identifier is omitted, but even omitting quotation marks, there is still a key string .
About the property name (key / property) and property values (value)
- Each key is the attribute name of the object
- Each attribute value is an object of value
Property name Example:
let obj = { 1:'a', 3.2:'b', 1e2:true, 1e-2:true, .234:true, 0xFF:true } Object.keys(obj) //可以得到obj所有的key = ["1", "100", "255", "3.2", "0.01", "0.234"]
Variable names and attributes do make constant attribute name:
= per the let 'name' the let OBJ1 = {per: 'bubu_sourire'} // here attribute name is "per" the let obj2 = {[per]: 'bubu_sourire'} // here is the name attribute "name"
note:
- Without [] will automatically become the property name string
- Plus [] will be evaluated as a variable; If the value is not a string, the string will automatically become
Hidden property of an object
Common attributes prototype chain
Deletions object properties change search
Delete property
- Delete the obj property xxx
delete obj.xxx delete obj.['xxx']
- Free property name
'xxx' in obj === false
- It contains the attribute name, but refers to the undefined
'xxx' in obj && obj.xxx = undefined
note:
obj.xxx === undefined can not be concluded 'xxx' whether obj property
View property
Two ways to view properties
obj.key // dot syntax obj. [ 'key'] // bracketed syntax obj. [key] // this is the key variable acquisition target value, the difference between this syntax (!!! easy to step on the pit, please be careful! !!)
note:
- obj.name equivalent to obj [ 'name'] (string).;
- obj.name not equivalent to obj [name] (variable value).;
- Here name is a string, not a variable.
- let name = 'bubu_sourire' => obj. [name] is equivalent to obj. [ 'frank']
Comparative Example
person.name
person[name]
- View all own property
Object.keys(obj)
- And their common attributes View
console.dir (obj)
Or printed out successively with Object.keys obj .__ proto__
- Judgment is itself a property or a total of
let obj = {'aaa':111} obj.hasOwnProperty('aaa') //true obj.hasOwnProperty('toString') //false
Modifying or adding attributes
Direct assignment
obj = {name the let: 'AAA'} // name string obj.name = 'BBB' // name string obj [ 'name'] = 'CCC' obj [ 'Na' + 'Me'] = 'ddd' obj [name] = 'Eee' // !! undefined error !! name the let Key = 'name' obj. [Key] = 'ddd' // equivalent to obj [ 'name'] obj. Key = 'ddd' // !! !! obj.key error here is equivalent to obj. [ 'key']
Batch assignment
Object.assign(obj,{age:18,gender:'man'})
Modifying or adding common attributes
Can not by itself modify or increase common attributes, change can only change their own property
You can use obj .__ proto __. ToString = ' xxx' modify common attributes, but not recommended
Recommended Object.create
let obj = Object.create(common)
let obj = Object.create(common,{key1:{value:'value1'},key2:{value:'value2'}})