1.解构赋值-数组篇 //Destrcturing(解构)
//ES5
/*
var a = 1;
var b = 2;
var c = 3;
*/
//ES6
var [a,b,c] = [1,2,3];
console.log(b);
//demo2
let [
foo,
[
[bar],
base
]
] = [
1,
[
[2],
3
]
];
console.log(bar);//2
console.log(base);//3
//demo3
let [,a,] = [1,2,3];
console.log(a);//2
//demo4
let [zero,...j] = [0,1,2,3,4]; //注意是三个.
console.log(j); //[1,2,3,4]
//demo5
let [a,b] = [1];
console.log(a); //1
console.log(b); //undefined
//demo6 不完全解构
let[a,[b],c] = [1,[2,3],4];
console.log(b); //2
console.log(c);//4
//demo7 制定默认值
var [temp = 'jewave'] = [];
console.log(temp);//'jewave'
// var [temp='jewave'] = ['xiang'];
// var [p,q = 'aaa'] = ['bbb',undefined] 【'bbb','aaa'】
// var [temp] = 1; Iterator接口之非遍历结构报错 var [temp]= undefined;报错
//demo8 Iterator接口之可遍历结构
let [a,b,c] = new Set(['x','y','z']);
console.log(b); // 'y'
function* fibs() {//指针函数
let a = 0;
let b = 1;
while (true) {
yield a;
[a,b] = [b,a+b];
}
};
var [fir,sec,thi,fou,fif,six] = fibs();
console.log(six); //5