ES6--函数的参数

参数展开(扩展)

1、收集剩余的参数

1 function show(a, b, ...args) {
2     console.log(a);
3     console.log(b);
4     console.log(args);
5 }
6 
7 show(1, 2, 3, 4, 5)

打印结果如图。args为数组。

 位置必需在参数的最后一个(rest parameter 剩余参数)

1 function show(a, b, ...args, c) {
2     console.log(a);
3     console.log(b);
4     console.log(args);
5 }
6 //报错    Rest parameter must be last formal parameter

 2、展开数组

  展开后的效果就是直接将数组内容拿出来

let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let arr = [...arr1, ...arr2]
console.log(arr);   //[1, 2, 3, 4, 5, 6]

默认参数

1 function show(a, b = 1, c = 2) {
2     console.log(a, b, c)
3 }
4 
5 show(5) //5 1 2
6 show(6, 9)  //6 9 2

 

posted @ 2018-06-11 16:01  懒懒同学  阅读(447)  评论(0编辑  收藏  举报