-
Notifications
You must be signed in to change notification settings - Fork 0
/
Closures.js
38 lines (33 loc) · 874 Bytes
/
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
/* 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 */
// function init() {
// 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
// }
// name = "Harry"
// return displayName;
// }
// let c = init();
// c()
function 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 = 999
y()
}
return x
}
let a = returnFunc()
a()