参数展开运算符

形参展开

像这样实参1传递给了n,而剩下的2,3,4,5等等,都传递给了数组m

function multiply (n:number,...m:number[]){
    return m.map((x) => x*2)//[ 4, 6, 8, 10 ]
}
console.log("🚀 ~ file: 15-参数展开运算符.ts ~ line 5 ~ multiply(1,2,3,4,5)", multiply(1,2,3,4,5))
multiply(1,2,3,4,5)

实参展开

像这样可以把实参arr2展开

const arr1 = [1,2,3];
const arr2 = [4,5,6];
arr1.push(...arr2)
console.log(arr1);///[ 1, 2, 3, 4, 5, 6 ]
posted @ 2022-02-22 10:07  xuelin  阅读(33)  评论(0编辑  收藏  举报