-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure.html
More file actions
85 lines (65 loc) · 2.66 KB
/
closure.html
File metadata and controls
85 lines (65 loc) · 2.66 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> laxical scoping and Closure</title>
</head>
<body style="background-color: #313131;">
<button id="orange">Orange</button>
<button id="green">Green</button>
</body>
<script>
// function init(){
// let name = "Mozilla";
// function displayName() {
// console.log(name);
// }
// displayName();
// }
// init()
// lexical scoping
// function outer(){
// let username = "sandeep"
// // console.log("OUTER", secret);
// function inner(){
// // let secret = "my123"
// console.log("inner", username);
// }
// function innerTwo(){
// console.log("innerTwo", username);
// // console.log(secret);
// }
// inner();
// innerTwo();
// }
// outer()
// // console.log("TOO OUTER", username)
// Closure
// function makeFunc(){
// const name = "Mozialla";
// function displayName() {
// console.log(name);
// }
// return displayName; // displayName func get reference of full outer func by using return key word
// }
// const myFunc = makeFunc();
// myFunc();
</script>
<script>
// document.getElementById('orange').onclick = function(){
// document.body.style.backgroundColor = 'orange';
// }
// document.getElementById('green').onclick = function(){
// document.body.style.backgroundColor = 'green';
// }
function clickHandler(color) {
// document.body.style.backgroundColor = `${color}` // without using closure the whole body color change without click the btn for excuting the color on clicking the btn we use closure
return function(){
document.body.style.backgroundColor = `${color}`
}
}
document.getElementById('orange').onclick = clickHandler('orange')
document.getElementById('green').onclick = clickHandler('green')
</script>
</html>