1function foo() {
2 for (var i = 0; i < arguments.length; i++) {
3 console.log(arguments[i]);
4 }
5}
6
7foo(1,2,3);
8//1
9//2
10//3
1function sum(...values) {
2 console.log(values);
3}
4sum(1);
5sum(1, 2);
6sum(1, 2, 3);
7sum(1, 2, 3, 4);
8
9
10function sum(...values) {
11 let sum = 0;
12 for (let i = 0; i < values.length; i++) {
13 sum += values[i];
14 }
15
16 return sum;
17}
18console.log(sum(1)); //1
19console.log(sum(1, 2)); //3
20console.log(sum(1, 2, 3)); // 5
21console.log(sum(1, 2, 3, 4)); //10
1function add() {
2 var sum = 0;
3 for (var i = 0, j = arguments.length; i < j; i++) {
4 sum += arguments[i];
5 }
6 return sum;
7}
8
9add(2, 3, 4, 5); // 14
10