-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.js
30 lines (22 loc) · 850 Bytes
/
closure.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
/*
Closure happens when a function retains access to variables of its outer (enclosing) function, even after that outer function has completed execution
Closure is a function bundled together with reference to its surrounding state (lexical environment)
When a function is returned from a another function, it still remember the references it was pointing too, its not just that function alone is returned but its closure is returned.
It remembers reference of the variable but not the value.
uses/applications:
1. memoization
2. currying
3. debounce/throttle
4. once function
5. Data hiding & encapsulation
*/
function closure() {
let greet = "Hello World";
return function() {
console.log(greet);
greet = "Hey there";
}
}
const func = closure();
func();
func();