-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path29.3 Corner Cases in Closures in JS.html
60 lines (55 loc) · 1.98 KB
/
29.3 Corner Cases in Closures in JS.html
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
<!DOCTYPE html>
<html lang="en">
<head><title>Closures in JavaScript</title>
</head>
<body>
<strong>
See Program 29,29.1,29.2 before this program
</strong>
<script>
//Closures came with lot of corner cases/side things watch at 13:15
// case 1/extra things
function x(){
var a=10;
function y(){
console.log(a);
}
//changing value of a
a=50;
return y;
}
var z=x();
console.log(z);
/*****/
z(); // o/p:10 before changing the value of a and after changing its value in line number 20
//output will be : 50 , Actually its not value of 'a' which is returned
//its reference of 'a' which is returned, so the function remember the reference to the variable 'a'
//case 2/extra things-2 (suppose above code was inside the function )
//lets see what happens when you access varibale of another level parent from the deapest function
console.log("case 2:");
function outer(){
var b=30;
function x(){
var a=10;
function y(){
console.log(a,b); // 10 30
// it form a closure along with 'a' which is its parent and also with 'b' which is its parents parent
}
y();
}
x();
}
outer();
// uses of closures:
// 1) module design pattern
// 2) currying
// 3) functions like once(a function which is called only once)
// 4) memoize
// 5) maintaining state in async world
// 6) setTimeouts
// 7) Iterators
// 8) and many more, see on google
// closures in JS are everywhere , the came naturally
</script>
</body>
</html>