-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathClosures.js
139 lines (113 loc) · 3.67 KB
/
Closures.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* 💡"JavaScript-with-JC"
👉 Closures, Function along with it's lexical scope bundled together to form a closure.
In simple words, Each and every function in javascript has access to its outer function's scope.
Inner function can access all the variables and functions of it's outer function
even after the execution context of outer function has been completely removed from the call stack.
*/
// 💡Example -
const outer = () => {
var a = 99;
const inner = () => {
console.log(a);
};
return inner;
};
console.log(outer()); // [Function: inner]
const getInner = outer(); // In this line, execution context of
// outer function will be deleted from callstack.
getInner(); // 99 => Even after outer fn is completely removed,
// inner fn has access to it's varaiable a here.
// 💡Uses of Closures
// 1) Memoization 👇
// Memoizing the result of the function and if later the same function is called with same arguments
// then return memoized result instead of executing whole function again
const memoizedMultiplyBy100 = () => {
const cache = {};
return (num) => {
if (cache[num]) {
return cache[num];
}
const result = num * 100;
cache[num] = result;
return result;
};
};
// Here, inner return function has access to cache object of outer function
const multiplyBy100 = memoizedMultiplyBy100();
console.log(multiplyBy100(1)); // 100
console.log(multiplyBy100(2)); // 200
console.log(multiplyBy100(3)); // 300
console.log(multiplyBy100(4)); // 400
console.log(multiplyBy100(2)); // 200
// 2) Module Design Pattern 👇
// All the members and methods of a particular module should be encapsulated together to maintain the well-structured code
const todosModule = (() => {
let todos = ["office", "gym", "party", "drive"];
const getTodos = () => {
return todos;
};
const addTodo = (newTodo) => {
todos.push(newTodo);
return todos;
};
const deleteTodo = (todoName) => {
const findIndex = todos.indexOf(todoName);
todos.splice(findIndex, 1);
return todos;
};
return {
getTodos,
addTodo,
deleteTodo,
};
})();
// console.log(todos); // todos is not defined
console.log(todosModule.getTodos());
// [ 'office', 'gym', 'party', 'drive' ]
console.log(todosModule.addTodo("sleep"));
// [ 'office', 'gym', 'party', 'drive', 'sleep' ]
console.log(todosModule.deleteTodo("gym"));
// [ 'office', 'party', 'drive', 'sleep' ]
// 3) Famous setTimeout question 👇
(() => {
for (var i = 0; i < 5; i++) {
setTimeout(() => {
console.log(i);
}, i * 1000);
}
})();
// this will print 5(0th sec) 5(1st sec) 5(2nd sec) 5(3rd sec) 5(4th sec)
// Now, Let's use closure to get expected output, we need to just wrap the setTimeout in function
// outer function wrapper will take different arguments each time from 0 to 4,
// and because of closure, inner setTimeout will points to different i( from 0 to 4 )
(() => {
for (var i = 0; i < 5; i++) {
const wrapper = (i) => {
setTimeout(() => {
console.log(i);
}, i * 1000);
};
wrapper(i);
}
})();
// this will print 0(0th sec) 1(1st sec) 2(2nd sec) 3(3rd sec) 4(4th sec)
// 💡Advantages of Closures
// 1) Data hiding, Encapsulation, variables can not be access or modified directly from outside the functions 👇
function Person() {
var name = "Jay";
this.getName = () => {
return name;
};
this.setName = () => {
name = "Jc";
};
}
const jayesh = new Person();
// console.log(name); // name is not defined
console.log(jayesh.getName()); // Jay
jayesh.setName();
console.log(jayesh.getName()); // Jc
// 💡Disadvantages of Closures
// 👉 Overconsumption of memory.
// 👉 Variables are not Garbage collected.
// 👉 Memory leaks.