参数扩展
1.收集剩余参数到一个数组中
function show(a,...args){ //...args必须为最后一个形参
console.log(a)
console.log(args)
}
show(2,13,14) //2 [13,14]
2.数组展开(...arr 表示把数组arr展开)
例1
let arr=[1,2,3]
function show(a,b,c)
{
console.log(a,b,c)
}
show(...arr) //等价于show(1,2,3)
例2
let arr1=[1,2,3]
let arr2=[5,6,7]
let arr=[...arr1,...arr2]
console.log(arr) //[1,2,3,5,6,7]
例3
function show(...args)
{
fn(...args)
}
function fn(a,b)
{
console.log(a+b)
}
show(3,4) //7
默认参数
function show(a,b=5,c=12)
{
console.log(a,b,c)
}
show(99) //99 5 12
show(99,100,101) //99,100,101