三点运算符 (...)的用法
1:展开运算符
let a=[1,2,3]; let b=[0,...a,4];//[0,1,2,3,4] let obj ={a:1,b:2}; let obj2 ={...obj,c:3}; //{a:1,b:2,c:3}; let obj3 ={...obj,a:3};//{a:3,b:2}
2:剩余操作符,把剩余的东西放到一个array里面赋值给它
let a=[1,2,3]; let [b,...c]=a; //b --1,c --[2,3] let a=[1,2,3]; let [b,...[c,d,e]]=a; b; // 1 c; // 2 d; // 3 e; // undefined function test(a,...rest){ console.log(a);//1 console.log(rest);//[2,3] } test(1,2,3)
let array =[1,2,3,4,5]; const {x,y,...z}=array; //其中z=[3,4,5],注意如果由于array的length不足以完成析构,则z为【】 对象 let obj={name:'zhangsan',age:30,city:'shenzhen'}; const {name,...others}=obj; console.log(name);//'zhangsan' console.log(others);//{age:30,city:'shenzhen'}