Declare two syntax for objects
1.1 grammar
The first kind;let obj = { name: "frank".age: 18}; The second;let obj = new Object({ name: "frank"}); Anonymous objects:console.log({'name':"frank".age:18})
Copy the code
Note:
{foo : 'a'}
Copy the code
Is not the object
1.2 Writing method of attribute name
- The key name (attribute name) is always a string, not an identifier, and can contain any character
- Quotation marks can be omitted, and only identifiers (except full numbers) can be used after omission.
- Even if the quotes are omitted, the key is a string.
- You must enclose quotation marks when there are Spaces and special characters.
- There are no numeric keys or numeric subscripts because the browser automatically converts numbers to strings.
1.3 Variables as attribute names
If you want to use the value of the variable as the attribute name, enclose the attribute name with []
let a = 'xxx'
let obj = {[a] : 111}
Copy the code
contrast
- Attribute names without [] are automatically changed to strings
- [] is evaluated as a variable
- If the value is not a string, it tries to change to a string
2 How do I delete an object attribute
2.1 grammar
deleteObj. XXX ordelete obj['xxx']
Copy the code
2.2 Checking whether an attribute is an attribute of the object
Methods a'name' in obj // false cannot distinguish whether the property is its own or common to the stereotypeMethod 2 obj. HasOwnProperty ('name') // false
Copy the code
2.3 pay attention to
obj.xxx === undefinedCan't judge'xxx'Is an attribute of obj, if:'xxx' in obj && obj.xxx === undefined
Copy the code
Example:
var obj = {}
var obj2 = {x: undefined}
obj.x === undefined // true
obj2.x === undefined // true
Copy the code
3. How do I view the properties of an object
3.1 Viewing all properties of the Object
Object.keys(obj) / / check the keys
Object.values(obj) / / check values
Object.entries(obj) // check keys and values
Copy the code
3.2 Viewing the Object itself + Common Properties
console.dir(obj) // View its own properties as a directory
Copy the code
3.3 Determine whether an attribute is self-contained or common
'name' in obj // Cannot determine whether this attribute is a property of its own or a shared property
obj,hasOwnProperty('name') // true represents a property of its own
Copy the code
3.4 Two Methods to View Attribute Values
Parenthesis syntax: obj['key'[obj. Key]Copy the code
Pay attention to
obj[key] // the value of the variable key is indeterminate
obj[console.log('name')] // undefined because.log returns undefined
Copy the code
4 How can I modify or add attributes of an object
4.1 Direct assignment
let obj = {name: 'frank'} change: obj. Name = obj'an'
obj['name'] = 'an'
obj['na' + 'me'] = 'an'Or:let key = 'name'
obj[key] = 'an'
Copy the code
4.2 Batch Assignment
Object.assign(obj,{age: 18.gender: 'man'})
Copy the code
4.3 Modifying Common Attributes
- Common attributes cannot be modified or added by themselves
- To modify or add properties on the stereotype
obj.__proto__.toString = 'xxx' / / do not recommend
Object.prototype.toString = 'xxx'
obj.__proto__ === window.Object.prototype
Copy the code
4.4 Modifying the Prototype
obj.__proto__ = null / / do not recommend
let obj = Object.create(common) // Attributes are on the prototype
Copy the code