-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure.html
More file actions
66 lines (60 loc) · 2.23 KB
/
closure.html
File metadata and controls
66 lines (60 loc) · 2.23 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Closure</title>
</head>
<body style="background-color: #3232;">
<button id = "orange">Orange</button>
<button id = "green">Green</button>
</body>
<script>
// closure
/*A closure is the combination of a function bundled together (enclosed) with
references to its surrounding state (the lexical environment). In other words,
a closure gives a function access to its outer scope. In JavaScript, closures are
created every time a function is created, at function creation time.*/
/* function init() {
let name = "Mozilla"; // name is a local variable created by init
function displayName() {
// displayName() is the inner function, that forms a closure
console.log(name); // use variable declared in the parent function
}
displayName();
}
init();*/
/** function outer(){
let username = "hitesh"
console.log("Outer", secret)
function inner(){
let secret = "my2428" // no sharing between the inner fxn
console.log("inner", username);
}
function innerTwo(){
console.log("innerTwo", username);
}
inner()
}
outer()
console.log(username);
*/
/*function makeFunc() {
const name = "Mozilla";
function displayName() {
console.log(name);
}
return displayName; //here the inner fxn is returned
}
const myFunc = makeFunc(); // this function is the reference of the displayname() so as it has lexical scoping it will also carry the scope of its outer fxn
myFunc();*/
</script>
<script>
document.getElementById("orange").onclick = function(){
document.body.style.backgroundColor = `orange`
}
document.getElementById("green").onclick = function(){
document.body.style.backgroundColor = `green`
}
</script>
</html>