解构
ES6中允许按照一定模式,从数组和对象中提取之,对对象进行复制,这被称为解构
基本用法
let [a,b,c] = [1,2,3];
console.log(a);
console.log(b);
console.log(c);
解构不成功
let [a,b] = [1];
console.log(b);
如上,如果解构不成功,b的值为undefined
默认值
let [a,b=2] = [1];
console.log(b);
对象解构赋值
let {a,b} = {b:"2",a:"3"};
console.log(a);
console.log(b);
函数参数的解构赋值
function add([x,y]){
return x+y;
}
let c = add(["c","sd"]);
console.log(c);