-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosures1.js
More file actions
63 lines (53 loc) · 1.12 KB
/
closures1.js
File metadata and controls
63 lines (53 loc) · 1.12 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
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
//Closure: A function bind together with its lexical environment!
// function x(){
// var a=7;
// function y(){
// console.log(a);
// }
// y();
// }
// x();
//Functions to a variable
// function x(){
// var a=function y(){
// console.log(a);
// };
// y();
// }
// x();
//You can Pass the function inside a funtion
// function x(){
// var a=7;
// y();
// }
// x(function y(){
// console.log(a);
// });
//Return the function Y instead of calling it
// function x(){
// var a=7;
// function y(){
// console.log(a);
// }
// return y;
// }
// x();
function x(){
var a=7;
function y(){
console.log(a);
}
return y;//funtion+lexcial environment whole thing was returned
}
let z=x();//whatever is returned is then collected here
console.log(z);//printed
//..........1000 lines after if i invoke a function
z();
//when u execute z somewhere else in your program it still remembers the ref to a and try to find value of a
// function x(){
// var a=7;
// return function y(){
// console.log(a);
// }
// }
// x();