1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
{ //数组解构赋值 let [a,b,c,[d,e=5]]=[1,2,3,[4]]; console.log(a,b,c,d,e) } { function* fibs() { var [a,b]=[0,1]; while (true) { yield a; [a,b]=[b,a+b]; } } var [first,second,third,fourth,fifth,sixth]=fibs(); console.log(sixth);//5 } { //对象解构赋值 var {foo:baz,bar}={foo:"aaaa",bar:"bbbb"}; console.log(baz); console.log(bar); } { //字符串解构赋值 const [a,b,c,d,e]="hello"; console.log(a,b,c,d,e); let {length:len}="hello"; console.log(len); } { //数值、布尔值解构赋值 let {toString:s} = 123; let {toString:t} = true; console.log(s === Number.prototype.toString); console.log(t === Boolean.prototype.toString); } { //函数参数解构赋值 var tmp=[[1,2],[3,4]].map(([a,b])=>a+b); console.log(tmp); function move({x=0,y=0}={}) { return [x,y]; } move({x:3,y:6}); move({x:3}); move({}); move(); function go({x,y}={x:0,y:0}) { return [x,y]; } go({x:6,y:9}); go({x:6}); go({}); go(); } { var map = new Map(); map.set('first','hello'); map.set('second','world'); for(let [key,value] of map){ console.log(key+" is "+ value); } for(let [key] of map){ console.log(key); } for(let [,value] of map){ console.log(value); } } |