Object’s function:
Definition of an object: What is an object and is an object a complex data type
The object is used to store multiple data as key-value pairs
Similarities and differences between objects and arrays
(1) Similarities: Both are complex data types and can store multiple data
(2) Differences: Different storage methods
-
Array: Ordered storage
-
Object: Unordered storage (key-value pairs)
Object syntax
A statement object
Let object name = {attribute name: attribute value, attribute name: attribute value,}Copy the code
Object value
Syntax: object name. attribute name
Features:
-
Object name [‘ Property name ‘] resolves to property name if there is a string inside []
-
Object name [variable name] If there is no string inside [], it is resolved to the variable name
Details: What data type is an attribute value in an object that can be retrieved using all the syntax of that type
-
Object whose property value is an array can be: object name. Attribute name [subscript]
-
If the attribute value of an object is a function, it can be: object name. The attribute name ()
Object assignment
Syntax: (1) Object name. Attribute name = value (2) Object name [‘ attribute name ‘] = value
Attributes :(1) if the attribute name exists, it means’ modify ‘the attribute value. (2) if the attribute name does not exist, it means’ add’ the attribute
(3) Delete object attributes :delete object name. The property name
Object traversal
Syntax: for(let key in object name){object name [key]}
let obj = {
name: 'summer'.age: 18.sex: 'woman'.hobby:'learning'
}
for( let key in obj){
// Consider: key is a variable. What syntax should be used to fetch the value of an object's attribute
console.log( key )//'name' 'age' 'sex' 'hobby'
// console.log( obj[ 'key' ] )//undefined
// console.log( obj.key )//undefined
console.log( obj[ key ] )//'summer' 18 'nv'
}
Copy the code