1.复制
//数组的复制 var arr1 = ['hello'] var arr2 =[...arr1] arr2 // ['hello'] //对象的复制 var obj1 = {name:'chuichui'} var obj2 ={...obj1} ob12 // {name:'chuichui'}
2.合并
//数组的合并 var arr1 = ['hello'] var arr2 =['chuichui'] var mergeArr = [...arr1,...arr2] mergeArr // ['hello','chuichui'] // 对象分合并 var obj1 = {name:'chuichui'} var obj2 = {height:176} var mergeObj = {...obj1,...obj2} mergeObj // {name: "chuichui", height: 176}
3.字符串转数组
var arr1 = [...'hello'] arr1 // ["h", "e", "l", "l", "o"]
4.函数传参
function f(v,w,x,y,z){ } var args = [2,3] f(1,...args,4,...[5])
数组中的元素迭代为函数参数时
function f(x,y,z){} var args = [1,2,3] f(...args) // 以前的方法 f.apply(null,args);