///解构数组的各种情况,按位置顺序取 let [x,y,z]=[1,2,3]; console.log(x,y,z); let [a1,[b1,c1]]=[10,[12,14]; console.log(a1,b1,c1); let [,,nice]=['YES','NO','PASS']; console.log(nice); let [p1,,p3]=[3,6,9]; console.log(p1,p3); //...end表示所有参数,begin表达1 let [begin,...end]=[1,2,3,4,5,6,7,8,910]; console.log(begin); console.log('============='); console.log(end); //y1会是undefined ,z1会是空数组 let [x1,y1,...z1]=['a']; console.log(x1,y1,z1); let [xxxx] = []; let [xxx,yyy]=[1]; console.log(xxxx,yyy); let [j,k]=[7,8,9]; console.log(j,k); let [L1,[L3],L4]=[1,[2,3],4]; console.log(L1,L3,L4); ///下面会报错,只能赋值数组 //let [pp1]=1; //let [pp2]=true; // let [pp3]=NaN; // let [pp4]=undefined; // let [pp5]=null; // let [pp6]={}; // @ts-ignore let [q1,q2,q3]=new Set(['aaa','bbb','ccc']); console.log(q1,q2,q3); //生成器 function* fibs(){ let a=0; let b=1; while (true){ yield a; [a,b]=[b,a b];///返回前数和后数的队列 0 1 1 2 3 5 8 } } let [m1,m2,m3,m4,m5,m6] = fibs()//从生成器中取6次 console.log(m1,m2,m3,m4,m5,m6); //生成器 function* test(){ let x=0; while (true){ yield x; x =2.//第一次返回2 } } let [cx1,cx2,cx3]=test()//从生成器中取3次 console.log(cx1,cx2,cx3);//- 0 2 4 ///解构默认值 let [posX=0,posY=0]=[]; console.log(posX,posY); let [meta1,meta2='a']=['b']; console.log(meta1,meta2); let [meta3,meta4='xxx']=['yyy',undefined]; console.log(meta3,meta4); //undefined默认值生效 let [defval=1]=[undefined]; console.log(defval); //null默认值无效 let [defval2=1]=[null]; console.log(defval2); function f(){ console.log('aaa'); } let [zzz=f()]=[1]; console.log(zzz); let [zzz1=f()]=[undefined];///函数未返回的话取undefined console.log(zzz1); //对象解构,根据对象属性名取,与位置无关 let {name,age,address}={name:'RemoteDev',age:18,address:中国上海}; console.log(name,age,address); let {age1,name1,address1}={name1:'RemoteDev',age1:18,address中国上海}; console.log(name1,age1,address1); let {age2,name2,NewAddress}={name2:'RemoteDev',age2:18,address二、中国上海}; console.log(name2,age2,NewAddress);//会拿不到NewAddress因为它不同于对象名,NewAddress返回undefined //通过别名的形式来取 let {age3,name3,address3: NewAddress2}={name3:'RemoteDev',age3:18,address三、中国上海}; console.log(name3,age3,NewAddress2)///这样可以正常取address3值并赋值给NewAddress2 console.log(用别名完成取对象属性值); let personData = {UserName:'RemoteDev',Age:18,Address:中国上海}; let {UserName:_n,Age:_a,Address:_d}=personData; console.log(_n,_a,_d); //console.log(UserName); //UserName is not defined 因为UserName用于匹配对象属性名,将值放入变量_n使用别名时,只能使用别名变量 let tmpObj={ p:[ 'Ada', { 'cap':'Mix' } ] } //p用于匹配对象属性p,v1取Ada值,cap取Mix值 let {p:[v1,{cap}]}=tmpObj; console.log(v1,cap); ///匹配别名 let {p:[r1,{cap:r2}]}=tmpObj; console.log(r1,r2); //取整个对象值p let {p,p:[val1,{cap:val2}]}=tmpObj; console.log(p,val1,val2); ////嵌套对象的匹配和值 let node = { loc: { start:{ line:2, column:10 } } } let {loc,loc:{start},loc:{start:{line}}} = node; console.log(loc); console.log(start); console.log(line); let _Obj={}; let _Arr=[]; ({propA:_Obj.propA,propB:_Arr[0]}={propA:890,propB:false});//添加()否则会与变量名相匹配_Obj,_Arr冲突 console.log(_Obj,_Arr); //对象解构默认值 let {theKinds=9988}={}; console.log(theKinds); let {kind1,kind2=5}={kind1:9}; console.log(kind1,kind2); let {play:pname='Mick'}={play:'Linux'}; console.log(pname); let {play:{subName='Default'}}={play:{subName:undefined}}; console.log(subName); let {PI=3.14159}={PI:null};///这里默认值不生效 console.log(PI); let {_pa,_pb}={VLCA:100,VLCB:200}; console.log(_pa,_pb);///这里得不到值,因为匹配属性名不成功 let moveAction= {actionType:'Out'}; console.log(moveAction.actionType); let {log,sin,cos}=Math; ///解构数组属性 let numArr=[1,2,3]; let {0:first,[numArr.length-1]:last}=numArr; console.log(first,last); ///字符串解构 const [nx1,nx2,nx3]='ABC'; console.log(nx1,nx2,nx3); ///字符属性解构 let {length:len}='RemoteDev'; console.log(len); let {toString:s}=123; console.log(s); let {toString:s1}=true; console.log(s1); //因为undefined和null不能转换为对象,以下两种表达式会报错 //let {proA:__X}=undefined; //let {proB:__Y}=null; ///函数参数解构 function Add([x,y]){ return x y; } let result = Add(6,9); console.log(result); [1,2],[3,4].map(([x,y])=> console.log(x y)); ///函数参数默认值 function funcDef({x=0,y=0}={}){ console.log([x,y]); return [x,y]; } funcDef(); funcDef({x:50}); funcDef({x:10,y:20}); funcDef({y:100}); funcDef({}); ///变量值交换 let NumberA=10; let NumberB=20; [NumberA,NumberB]=[NumberB,NumberA]; console.log(NumberA,NumberB); function f1() { return [5,10,15]; } let [e1,e2,e3]=f1(); console.log(e1,e2,e3); function getTestObj() { return { a:5*2, b:10*2, c:15*2 } } let {a:ca1,b:ca2,c:ca3}=getTestObj(); console.log(ca1,ca2,ca3); 数组参数有序 function f2([x,y,z]) { console.log(x,y,z); } f(9,8,7); //对象参数无序 function f3({x=0,y=0,z=0}={}) { console.log(x,y,z); } f3({x:3,z:9,y:80}); ///通过解构提取JSONovrn let jsondaa = {
_id:200,
_status:"Success",
_data:[168,578]
};
let {_id,_status,_data:arr}=jsondata;
console.log(_id,_status,arr);
//构造Map
let myMap = new Map();
myMap.set(1,100);
myMap.set(2,"OK");
myMap.set(3,false);
myMap.set('UI','ZJY');
console.log(myMap);
//遍历Map
for (const [k,v] of myMap) {
console.log(k,v);
}