-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.html
More file actions
38 lines (32 loc) · 854 Bytes
/
index.html
File metadata and controls
38 lines (32 loc) · 854 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
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Closures</h1>
<p>Counting with a local variable.</p>
<p id="demo">0</p>
<button type="button" onclick="myFunction()">Count!</button>
<button type="button" onclick="reset()">Reset</button>
<script>
function myCounter() {
let counter = 0;
return {
increment: function () {
counter++;
return counter;
},
reset: function () {
counter = 0;
return counter;
},
};
}
const counterObj = myCounter();
function reset() {
document.getElementById("demo").innerHTML = counterObj.reset();
}
function myFunction() {
document.getElementById("demo").innerHTML = counterObj.increment();
}
</script>
</body>
</html>