object
It can be summarized from the following aspects :
- An array is an ordered set of data
- An object is an unordered set of data , Have attribute methods
- attribute : The data the object owns
- Method : Object owned operations
- Built-in objects : Array set map Functions, etc .....
let odj = { name(key key ):25( value )}
- When using properties that do not exist in an object , You'll get undefined
How to create an object
- Declared quantity let
obj = { }
; - Constructors
let obj = new object( );
The value corresponding to the key of a piece of data in the object is not a function , This data is called “ attribute ”
What properties does an object have :
increase :
- Create with
example :let obj = {
name: zhangsan, // attribute
age: 18 // attribute
talk(){ // Method
console.log(666)// 666
}
}
obj.talk() // It is to call this method to output 666
- obj.key = value example :obj.job = "loser"
- obj[string] = value example :obj["gender"] = "male"
- console.log(obj.age); //18
let age = Symbol(“ You can write notes ”); // Make a statement symbol Variable
obj[age] = 14; // to symbol Variable assignment ( Tell him to add )
console.log(obj[age]); //age:14
console.log(obj) // { name: 'zhangsan', age: 18, [Symbol(“ You can write notes ”)]: 14 }
( If you don't want to change the property value But also want to add the same name key It can be written like this )
Delete :
delete obj.key || remove obj.key
Delete method delete obj. Method name ( Don't put parentheses );
Using the deleted attribute will undefined Calling the deleted method will report an error
check :
console.log(obj.age)
Change :
obj.key = new value
example :
obj.gender = "man"
obj.gender = "female" // change
Change what you have , If not, add
example :talk: function() {
console.log(666)
}
obj.talk(); //666
External add method : obj.eat = function () {
console.log("2")
}
obj.eat(); //"2"
Access to the properties of an object must be through the object · Talent
example :let obj = {
name: "wang",
age: 18,
talk: function () {
console.log(` My name is ${obj.name}`)
}
}
obj.talk(); //"wang"
How to traverse the key value pairs of an object
let arr = {
name: "zhangsan",
age: 18,
talk() {
console.log(666);
}
}
for (let i of Object.keys(arr)) { // key
console.log(i); //name age talk
}
for (let i of Object.values(arr)) { // value
console.log(i); //zhangsan 18 [Function: talk]
}
for (let i of Object.entries(arr)) { // Key and value
console.log(i); //[ 'name', 'zhangsan' ]
[ 'age', 18 ]
[ 'talk', [Function: talk] ]
}
Determine whether there is a key to find in the object
console.log(“mao” in arr); // If not, a Boolean value is returned false
console.log(“age” in arr); // If so, it returns Boolean value true