解構賦值

解構對象

const tom = {
    name:'tom',
    age:21,
    height:170,
    greet:function () {
        return `hello ,I'm ${this.name}`
		// console.log(`hello, I'm ${this.name}`)
        // console.log(this)
    }
}
tom.greet();
let { name, age} = tom
name ='jerry'
console.log(name, age)

解構傳入的函數參數

const tom = {
    name:'tom',
    age:21,
    height:170
}
// const printName = (people) => {console.log(`my name is ${people.name}`)};
const printName = ({name}) => {console.log(`my name is ${name}`)}
printName(tom);

解構數組

const arr = [1,2,3]
const [first] = arr
const [,second] = arr
const [,,third] = arr
console.log(first)  //1
console.log(second) //2
console.log(third)  //3

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章