-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-04(RestParameter).html
More file actions
54 lines (47 loc) · 1.34 KB
/
3-04(RestParameter).html
File metadata and controls
54 lines (47 loc) · 1.34 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
<html>
<head>
<title>Rest Parameter</title>
</head>
<body>
<script>
// function sum1(x1, x2) {
// var y1 = x1 + x2;
// console.log(y1);
// return y1;
// }
// sum1(5, 7);
// function sum2(x1, x2, x3) {
// var y1 = x1 + x2 + x3;
// console.log(y1);
// return y1;
// }
// sum2(5, 7, 9);
// function sum3(x1, x2, x3, x4) {
// var y1 = x1 + x2 + x3 + x4;
// console.log(y1);
// return y1;
// }
// sum3(5, 7, 9, 11);
// rest parameter, java 메소드 오버로딩과 동일한 기능
// function sum(...args) { // ... : 매개변수를 배열로 받음
// var total = 0;
// // 방법1 : for of - 배열 합계 구하기
// // for(var x of args)
// // total += x;
// console.log(total);
// }
function sum(...args) {
var total = 0;
// 방법2 : forEach - 배열 합계 구하기
args.forEach(function(x, i) {
// console.log(x, i);
total += x;
})
console.log(total);
}
// sum(5, 7);
// sum(5, 7, 9);
sum(5, 7, 9, 11);
</script>
</body>
</html>