解构赋值

按照一定模式,从数组和对象中提取值,对变量进行赋值。

数组解构

let arr =[1,2,3];

let [a,b,c] = [1,2,3];

let [a,b,[c,d]] = [1,2, [3,4]];

let [a,b,[c]] = [1,2, [3,4]];    c会对应3

let [a,b,c] = [1,2, [3,4]];      c对应数组[3,4]

let [a,b,c,d] = [1,2, [3,4]];   d是undefined

let [a,b,c,d=5] = [1,2, [3,4]];   d是5

let [a,b,c,d=5] = [1,2, [3,4],6];   d是6,

对象解构

let user={

  name:'ff',

  age:34

};

let {name,age} = user; 

let {age,name} = user;   对象里面通过key去对应,数组是通过下标

字符串解构

let str = 'imoc';;

let [a,b,c,d] = str

字符串的解构其实就是对应的数组的解构

 

应用

 

posted @ 2021-10-10 15:06  anjing123  阅读(14)  评论(0编辑  收藏  举报