Skip to content

Commit 254895b

Browse files
example of closure
0 parents  commit 254895b

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Diff for: index.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link
8+
rel="stylesheet"
9+
href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css"
10+
/>
11+
<title>Advanced JavaScript</title>
12+
</head>
13+
<body class="container">
14+
<h3>Advanced JavaScript @CoderDost</h3>
15+
<div>
16+
<div class="grid">
17+
<div>
18+
<button id="btn1" onclick="counter1()">a</button>
19+
</div>
20+
<div>
21+
<button id="btn2" onclick="counter2()">b</button>
22+
</div>
23+
</div>
24+
25+
<input type="text" id="text1" />
26+
<button onclick="strAdder1()">Add String</button>
27+
28+
<p id="text-output1">p1</p>
29+
30+
<input type="text" id="text2" />
31+
<button onclick="strAdder2()">
32+
Add String
33+
</button>
34+
35+
<p id="text-output2">p2</p>
36+
</div>
37+
38+
<script src="index.js"></script>
39+
<script src="test.js"></script>
40+
41+
</body>
42+
</html>

Diff for: index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
function initCounter(id) {
3+
let count = 0;
4+
return function () {
5+
count++;
6+
document.getElementById(id).innerText = count;
7+
};
8+
}
9+
let counter1 = initCounter('btn1');
10+
let counter2 = initCounter('btn2');
11+
12+
13+
14+
function initAddString(inputId, outputId) {
15+
let str = '';
16+
return function () {
17+
str += ' ' + document.getElementById(inputId).value;
18+
document.getElementById(inputId).value = '';
19+
document.getElementById(outputId).innerText = str;
20+
};
21+
}
22+
23+
let strAdder1 = initAddString('text1', 'text-output1');
24+
let strAdder2 = initAddString('text2', 'text-output2');

Diff for: test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)