-
Notifications
You must be signed in to change notification settings - Fork 0
/
closures.js
72 lines (48 loc) · 1.32 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
//
// A Closure is a combination of function bundled together(enclosed)
// to its surrounding state(the laxical environment). other words, closure
// gives u access to outer function's scope from an inner function
//
function init(){
let message = "Good Morning"
console.log("Regular Function msg: " + message)
function displayMessage(){
console.log("Message is: " + message + " from Closure")
}
message = "Good Evening"
return displayMessage;
}
// we need to put double parenthesis toinvoke closure
// init()()
// it can be done like this too
// c = init() // creating another variable
// c() // invoking with new var name
// different approach of closure
// function func() {
// var name = 'Mozilla'; // name is a local variable created by init
// function displayName() {
// // displayName() is the inner function, a closure
// console.log(name); // use variable declared in the parent function
// }
// displayName(); // function is called from inside
// }
// func();
returnFunc = () =>{
const x = () =>{
let a = 1
console.log(a)
const y = () =>{
// let a = 2
console.log(a)
const z = () =>{
// let a = 3
console.log(a)
}
z()
}
a = 564
y()
}
x()
}
returnFunc()