-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClosure.js
More file actions
280 lines (224 loc) · 5.3 KB
/
Closure.js
File metadata and controls
280 lines (224 loc) · 5.3 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// --------------------------------------------------------------------------
// Q-1: When passing methods as callbacks, the original context can be lost
const person = {
name: "Eve",
greet() {
console.log("Hi, " + this.name);
}
};
const greetFunc = person.greet;
// greetFunc(); // Undefined or error, because `this` is not bound to person.
// --------------------------------------------------------------------------
// Q1: Example of Closure - a function closing over a variable from its parent scope
function outer() {
let outerVar = 10;
function inner() {
console.log(outerVar);
}
return inner;
}
const closureFunc = outer();
// closureFunc();
// --------------------------------------------------------------------------
// Q2: Returning a function that remembers the passed parameter (Closure)
function greet(name) {
return function () {
console.log(`Hello, ${name}!`);
};
}
const sayHello = greet("sameer");
// sayHello();
// --------------------------------------------------------------------------
// Q3: Object with private state using closures
function counter() {
let count = 0;
return {
increment: function () {
count++;
console.log(count);
},
decrement: function () {
count--;
console.log(count);
},
getCount: function () {
return count;
},
};
}
// Usage Example:
// const counterInstance = counter();
// counterInstance.increment(); // 1
// counterInstance.increment(); // 2
// counterInstance.decrement(); // 1
// console.log(counterInstance.getCount()); // 1
// --------------------------------------------------------------------------
// Q4: Using setTimeout to delay a greeting
function delayedGreeting(name) {
setTimeout(function () {
console.log(`Hello, ${name}!`);
}, 1000);
}
// Usage Example:
// delayedGreeting('Bob');
// console.log('Greetings sent!');
// --------------------------------------------------------------------------
// Q5: Creating multiple functions that share a single counter variable
function createCounter() {
let count = 0;
const counters = [];
for (let i = 0; i < 3; i++) {
counters.push(function () {
console.log(count++);
});
}
console.log("counters array: ", counters )
return counters;
}
// const counters = createCounter();
// Usage Example:
// counters[0](); // prints 0
// counters[1](); // prints 1
// counters[2](); // prints 2
// --------------------------------------------------------------------------
// Q6: Various JavaScript hoisting and scoping examples
// Example 1: Implicit global declaration if 'var', 'let' or 'const' are not used properly.
// x = 4;
// console.log(x);
// Example 2: Declarations using let, var, const (Uncomment the one you want to test)
// let x;
// var x;
// const x;
// Another Example:
const a = 10;
// console.log(a + 1);
const fuinc = () => {
console.log(a + 2);
};
// console.log(a + 3);
// fuinc();
// Additional examples (commented out):
// Using closures:
// function closures() {
// const a = 10;
// function inside() {
// console.log(a);
// }
// inside();
// }
// closures();
// Arrow function with nested arrow functions:
// const closures2 = () => {
// const a = 10;
// const inside = () => {
// console.log(a);
// const inside2 = () => {
// console.log(a + 1);
// };
// inside2();
// };
// inside();
// };
// closures2();
// Currying examples:
// const a = (x) => {
// return (y) => {
// return (z) => {
// console.log(x + y + z);
// };
// };
// };
// // We can call this way:
// const and = a(2);
// const anb = and(3);
// anb(5);
// // Or using currying in a single expression:
// a(1)(2)(3);
// Scope chaining example:
// const x2 = 20;
// function a1() {
// var x1 = 10;
// b1();
// function b1() {
// c1();
// function c1() {
// console.log(x1);
// }
// }
// }
// // console.log(x1); // This would throw an error because x1 is not in the global scope
// console.log(x2);
// a1();
// --------------------------------------------------------------------------
// Practice Questions:
// 20 July 2024
// 01 September 2024
// ! Question: Create a count function that works as follows:
/*
count() // 1
count() // 2
count() // 3
count.reset()
count() // 1
count() // 2
*/
// Answer Implementation:
// const count = (() => {
// let counter = 0;
// function incr() {
// counter++;
// console.log(counter);
// return counter;
// }
// incr.reset = function () {
// counter = 0;
// };
// return incr;
// })();
// Usage Example:
// count();
// count();
// count();
// count.reset();
// count();
// count();
// let counterValue = 0;
// function count() {
// counterValue++;
// console.log(counterValue);
// return counterValue;
// }
// count.reset = function () {
// counterValue = 0;
// };
// count();
// count();
// count();
// count.reset();
// count();
// count();
function createCounter() {
let counter = 0;
function count(){
console.log(counter)
return counter++
}
count.reset = function(){
counter=0
}
return count
}
const count = createCounter();
count();
count();
count();
count.reset();
count();
count();
// Global Scope
// |
// |-- outer() Lexical Env
// |
// |-- Variables: { count: 0 }
// |
// |-- inner() ← keeps reference to this