//1.基本用法
let name ='张三',age =18, sex ='男';
let [name,age,sex] = ['李四',20,'女'];
name='qaq';
console.log(name);
console.log(age);
console.log(sex);
//02-对象的解构赋值
let {name, age, sex} = {name:"张三",age:55,sex:'男'};
console.log(name);
console.log(age);
console.log(sex);
//通过键值对的形式并不能完全解析
let {name:str} = {name:"张三"};
console.log(name);//无打印结果
console.log(str);//张三
//03数组的解构赋值
// let [name,age,sex] = ['李四',20,'女'];
let [arr1,[arr2,arr3,[arr4,arr5]]] = [1,[2,3, [4,5]]];
console.log(arr1, arr2, arr3, arr4, arr5); //1 2 3 4 5
let [a,b,c] = [1,2,3];
console.log(a); //1
console.log(b); //2
console.log(c); //3
let [a, ,c] = [1,2,3];
console.log(a); //1
console.log(c); //3
let [a, , ,c] = [1,2,3];
console.log(a); //1
console.log(c); //undefined
//04-基本类型的解构赋值
let [a,b,c,d,e] ='我是中国人';
console.log(a); //我
console.log(b); //是
console.log(c); //中
console.log(d); //国
console.log(e); //人
网友评论