Skip to content

Commit 0111bf7

Browse files
committed
Practicle example of closure/lexical scoping
1 parent 664c917 commit 0111bf7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<button id="orange">Orange</button>
10+
<button id="green">Green</button>
11+
</body>
12+
<script>
13+
function clickHandler(color){
14+
return function(){
15+
document.body.style.backgroundColor = `${color}`
16+
}
17+
}
18+
19+
document.querySelector("#orange").onclick = clickHandler("orange")
20+
document.querySelector("#green").onclick = clickHandler("green")
21+
</script>
22+
</html>

0 commit comments

Comments
 (0)