-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path47-Function_Closure.html
More file actions
42 lines (35 loc) · 963 Bytes
/
47-Function_Closure.html
File metadata and controls
42 lines (35 loc) · 963 Bytes
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Function Closure</title>
</head>
<body>
<script>
// Closure adalah kemampuan untuk mengeluarkan variabel dan function di local / nested scope agar dapat diakses di global scope
function createAdder(value) {
// local scope
const owner = "Eko";
function add(param) {
// nested scope
document.writeln(`<p>${owner}</p>`)
return value + param;
}
return add;
}
const addTwo = createAdder(2);
document.writeln(addTwo(10));
document.writeln(addTwo(6));
/*
seolah olah kita sedang membuat funstion seperti ini:
const owner = "Eko";
function addTwo(param) {
dodocument.writeln(`<p>${owner}</p>`);
return 2 + param;
}
*/
</script>
</body>
</html>