- 参数展开(扩展)
- 默认参数
参数展开
//参数展开,即收集剩余参数//...args是a b 之外的剩余参数function show(a, b, ...args){ console.log(a, b, ...args) }show(1,2, 'a', [3,4,5], {a:1,b:2})//输出 1 2 "a" (3) [3, 4, 5] {a: 1, b: 2}//参数展开,还可以展开一个数组function show(a, b, c){ console.log(a, b, c) }let arr = [1,2,3]show(...arr) //等价于 show(1,2,3),数组在此处展开//还可以如下let arr1 = [1,2,3]let arr2 = [4,5,6]let arr = [...arr1, ...arr2] //console.log(arr) // (6) [1, 2, 3, 4, 5, 6]//是不是很神奇
默认参数
//与其它语言类似function show(a, b=2, c='c'){ console.log(a,b,c) }show(1) // 1 2 "c"show() // undefined 2 "c"show(1,2,3) // 1 2 3