当前位置:网站首页>object

object

2020-11-08 23:47:00 Lsowind.

object
It can be summarized from the following aspects :

  1. An array is an ordered set of data
  2. An object is an unordered set of data , Have attribute methods
  3. attribute : The data the object owns
  4. Method : Object owned operations
  5. Built-in objects : Array set map Functions, etc .....
  6. let odj = { name(key key ):25( value )}
  7. When using properties that do not exist in an object , You'll get undefined

How to create an object

  1. Declared quantity let obj = { };
  2. 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 :

  1. 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

  1. obj.key = value example :obj.job = "loser"
  2. obj[string] = value example :obj["gender"] = "male"
  3. 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

版权声明
本文为[Lsowind.]所创,转载请带上原文链接,感谢