-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.html
97 lines (80 loc) · 2.58 KB
/
scope.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!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>Scope</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding: 2rem;
}
p {
padding: .5rem 0;
}
div {
width: 100%;
height: 100vh;
background-color: rgb(0, 195, 255);
font-weight: 800;
font-size: 2rem;
font-family: sans-serif;
text-transform: uppercase;
display: flex;
justify-content: center;
align-items: center;
color: #0e0e0e;
}
</style>
</head>
<body>
<script>
/*
## Scope
● Scope merupakan area akses sebuah data
● Ada dua jenis scope, global scope dan local scope.
● Setiap kita membuat function, maka kita akan membuat local scope untuk function tersebut
● Data di global scope bisa diakses dari local scope, namun data di local scope hanya bisa di akses di local scope tersebut atau di scope local dibawahnya (dalam kasus function dalam function)
*/
// =====Kode : Global Scope=====
document.writeln(`<p>=====Global Scope=====</p>`);
// Variable counter berada di Global Scope
let counter = 0;
// Function hitMe berada di Global Scope
function hitMe() {
// blok functionnya berada di local scope function hitMe
counter++; // We can access
}
hitMe();
hitMe();
document.writeln(`<p>${counter}</p>`);
// =====Kode : Local Scope=====
document.writeln(`<p>=====Local Scope=====</p>`);
function first() {
let firstVariable = "First";
}
function secound() {
let secoundVariable = "Secound";
}
first();
secound();
//console.log(firstVariable); // ERROR can not access local scope
//console.log(secoundVariable); // ERROR can not access local scope
// =====Kode : Nested Function Scope=====
document.writeln(`<p>=====Nested Function Scope=====</p>`);
function firstNested() {
let firstVariable = "First";
function secoundNested() {
document.writeln(`<p>${firstVariable}</p>`);
}
secoundNested();
}
firstNested();
</script>
</body>
</html>