-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.Rest Parameter.html
41 lines (34 loc) · 1.11 KB
/
9.Rest Parameter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- take a variable number of arguments.
取可变数量的参数。
allows us to apply map(), filter() and reduce() on the parameters array.
允许我们在参数数组上应用map()、filter()和reduce()。
-->
<script>
// 放置在形参上面的是不定参数
function howMany(...args) {
return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2)); // You have passed 3 arguments.
console.log(howMany("string", null, [1, 2, 3], {})); // You have passed 4 arguments.
const sum = (x, y, z) => {
const args = [x, y, z];
return args.reduce((a, b) => a + b, 0);
}
const sum2 = (...args) => {
const args1 = args;
return args1.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
console.log(sum2(1, 2, 3)); // 6
</script>
</body>
</html>