You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Notes/10-Closures.md
+13-5
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
# Episode 10 : Closures in JS
2
+
2
3
### Important Interview Question
3
4
4
-
**Closure :** Function bundled together with its lexical environment/scope.
5
+
**Closure :** Function bundled together with its lexical environment/scope.
5
6
6
7
```
7
8
JS is a weird language. You can pass functions as parameters to another function, assign a variable to an entire function, or even return a function.
@@ -19,9 +20,16 @@ console.log(z); // value of z is entire code of function y.
19
20
20
21
```
21
22
22
-
When functions are returned from another fun, they still maintain their lexical scope.
23
-
- When y is returned, not only is the fun returned but the entire closure (fun y + its lexical scope) is returned and put inside z. So when z is used
24
-
somewhere else in program, it still remembers var a inside x()
23
+
When functions are returned from another fun, they still maintain their lexical
24
+
scope.
25
+
26
+
- When y is returned, not only is the fun returned but the entire closure (fun
27
+
y + its lexical scope) is returned and put inside z. So when z is used
28
+
somewhere else in program, it still remembers var a inside x()
29
+
- Closure is a very powerful concept of JS, just because this function remembers
30
+
things even if they are not in their lexical scope
25
31
26
32
### Uses of Closure
27
-
Module Design Pattern, Currying, Functions like once(fun that can be run only once), memoize, maintaining state in async world, setTimeout, iterators...
33
+
34
+
Module Design Pattern, Currying, Functions like once(fun that can be run only
35
+
once), memoize, maintaining state in async world, setTimeout, iterators...
0 commit comments