forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosures.js
More file actions
35 lines (24 loc) · 1.2 KB
/
Copy pathclosures.js
File metadata and controls
35 lines (24 loc) · 1.2 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
/*
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 you access to an outer function’s scope from an inner function.
In JavaScript, closures are created every time a function is created, at function creation time.
*/
function modifyString(sampleString){
const modifier = function(){
const modifiedString = sampleString + ' is modified';
return 'Original String: --->'+ sampleString + ', Modified String: ---> ' + modifiedString;
}
return modifier;
}
const modifier = modifyString('sample string');
/*
function modifyString has two variables sampleString and modifier(function).
Now when modifyString is invoked, it returns modifier.
Usually, when a function is invoked, the memory allocated to the variables present inside gets freed
by the garbage collector. However, in javascript when a function returns another function
the returned function still has access to those variables. The reference to outer scope/environment
explains closures
*/
console.log(modifier());
// [out] Original String: --->sample string, Modified String: ---> sample string is modified