# rest参数
function test(a,b,...c){
console.log(a);
console.log(b);
console.log(c);
}
test(1,2,3,4,5,6,7,8)
//1和2赋给了a,b 345678赋给了c(数组)
//传进来后面的参数会被当成数组添加到c里面
//也可以把参数改成一个对象,打印c object
//...表示不知道传进来多少个参数
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 作用
实现加法
`function test(...c) {
let sum = 0;
for(let i = 0; i < c.length; i++){
sum += c[i];
}
console.log(sum);
}
test(1,2,3,4,5,6,7,8);//36
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 用在解构赋值里
# 数组
let a,b;
[a,...b] = [1,2,3,4,5,6,7,8];
console.log(a,b);
//b变成了数组2345678
1
2
3
4
2
3
4
# 字符串
let a,b;
[a,...b] = '123456789';
console.log(a,b)
//b数组["2","3","4","5","6","7","8","9"]
1
2
3
4
2
3
4