-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.js
More file actions
84 lines (68 loc) · 2.04 KB
/
Day10.js
File metadata and controls
84 lines (68 loc) · 2.04 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
// EP-10 | Closures in JS
// Closure: A function bind together with its lexical environment. A function along with its lexical scope
// forms a closure. Function y is bind to the variable of function x, so a closure is formed and it has the
// access to its parent's lexical scope.
// Example: closure
// global scope
function x() {
// outer scope
var a = 10;
function y() {
// inner scope
console.log(a); // 10
}
y();
}
x();
// MDN definition of closures: 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 us
// the access to an outer function's scope form an inner function. In JS closures are created every time
// a function is created, at function creation time.
// We can do all the same things which we can perform with the variables like we can assign a function to
// a variable, we can pass the function as an arg in a function, we can return a function from a function.
function temp() {}
// Allowed
function get(temp) {}
// Allowed
function send() {
return function () {};
}
// Allowed
const res = function (temp) {
return function () {};
};
// Due to these powers of function we call them First class functions in JS.
// Whenever a function returns a function then teh returned function will always returned with it attached
// lexical scope and the reference it forms with its parent's env will also be there. It can access those
// variables from anywhere at anytime.
// Corner cases:
function q() {
var w = 10;
function r() {
console.log(w);
}
w = 100; // This will re-initialize the value of w before returning the function
return r;
}
var result = q();
result(); // 100
function i() {
var h = 100;
function j() {
var g = 99;
function k() {
console.log(g, h); // 99 100
}
k();
}
j();
}
i();
// Uses:
// 1. Module design pattern
// 2. Currying
// 3. Once function
// 4. Memoize
// 5. maintaining state in async world
// 6. setTimeouts()
// 7. Iterators etc etc .....