-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure.html
More file actions
89 lines (67 loc) · 1.86 KB
/
Copy pathclosure.html
File metadata and controls
89 lines (67 loc) · 1.86 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>closure inside loops</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<script>
var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
funcs[i] = function() { // and store them in funcs
console.log("My value: " + i); // each should log its value.
};
}
/*for (var j = 0; j < 3; j++) {
funcs[j](); // and now let's run each one to see
}*/
// funcs[0]();
//
/*Example 4*/
function sayHello2(name) {
var text = 'Hello ' + name; // Local variable
var say = function() { console.log(text); }
return say;
}
var say2 = sayHello2('Bob');
say2(); // logs "Hello Bob"
function say667() {
var num = 42;
var say = function() { console.log(num); }
num = num + 2;
return say;
}
var sayNumber = say667();
sayNumber();
/*Example 5*/
/* function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + i;
result.push(function() { console.log(item + ' ' + list[i]) });
}
return result;
}
function testList() {
var fnlist = buildList([1, 2, 3]);
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
testList() */
/*Example 6*/
function sayAlice() {
var say = function() { console.log(alice);}
// Local variable that ends up within closure
var alice = 'Hello Alice';
return say;
//console.log(say);
}
sayAlice()(); // logs "Hello Alice"
</script>
</body>
</html>