一.什么是解構(gòu)
解構(gòu)就是從數(shù)組或?qū)ο?值類(lèi)型變量會(huì)事先調(diào)用.toString()方法)中獲取對(duì)應(yīng)屬性或值的方法鼻忠。
二.解構(gòu)有什么用
1.簡(jiǎn)化代碼,減少工作量凳兵,提升閱讀性
2.優(yōu)化賦值過(guò)程百新,簡(jiǎn)單易懂
三.怎么使用解構(gòu)
1.從數(shù)組賦值
let [x, y] = ["hello", "world"];
// x="hello", y="world"
2.從對(duì)象中獲取屬性
let {x, y: z} = {x: "hello", y: "world"}
// x = "hello", z = "world"?
//此處的y只是一個(gè)對(duì)應(yīng)的屬性名,真正的變量是z
3.參數(shù)賦值
function test({x, y}, ...z){
//x="hello", y="world", [...z] = ["i", "love", "china"]?
}
test({x:"hello", y:"world", z:"I LOVE CHINA"}, "i", "love", "china")