@@ -25,14 +25,14 @@ defined in. Since the only scope that JavaScript has is the
25
25
foo.increment();
26
26
foo.get(); // 5
27
27
28
- In the above example ` Counter ` returns ** two closures ** . The function ` increment `
29
- as well as the function ` get ` , both keep a ** reference** to the scope of
30
- ` Counter ` and therefore always have access to the ` count ` variable that was
31
- defined in that very scope.
28
+ In the above example ` Counter ` returns ** two** closures . The function ` increment `
29
+ as well as the function ` get ` . Both of these functions keep a ** reference** to
30
+ the scope of ` Counter ` and therefore always have access to the ` count ` variable
31
+ that was defined in that ** very** scope.
32
32
33
33
### Why private variables work
34
34
35
- Since it's not possible to reference or assign scopes in JavaScript, there's
35
+ Since it is not possible to reference or assign scopes in JavaScript, there is
36
36
** no** way of accessing the variable ` count ` from the outside. The only way to
37
37
interact with it is via the two closures.
38
38
@@ -45,30 +45,31 @@ This code will **not** change the variable `count` in the scope of `Counter`,
45
45
since ` foo.hack ` was not defined in ** that** scope, instead, it will create
46
46
(or override) the * global* variable ` count ` .
47
47
48
- One common mistake made about closures is that they do ** not** copy the
49
- variables of their outer scope, they ** reference** them.
50
-
51
48
### Closures inside loops
52
49
50
+ One mistake that is often made with closures is that they are used like if they
51
+ were copying primitive values, while they are in fact ** referencing** the
52
+ variables which hold those values.
53
+
53
54
for(var i = 0; i < 10; i++) {
54
55
setTimeout(function() {
55
56
console.log(i);
56
57
}, 1000);
57
58
}
58
59
59
- A common misconception is that the above will output the numbers ` 0 ` through
60
- ` 9 ` , while in reality, is simply prints the number ` 10 ` ten times.
60
+ The above will ** not ** output the numbers ` 0 ` through ` 9 ` , it will simply print
61
+ the number ` 10 ` ten times.
61
62
62
- The * anonymous* function keeps a reference to ` i ` and at the time ` conole .log`
63
- gets called the ` for loop ` has already finished and updated the value of ` i ` to
63
+ The * anonymous* function keeps a reference to ` i ` and at the time ` console .log`
64
+ gets called, the ` for ` loop has already finished and updated the value of ` i ` to
64
65
` 10 ` .
65
66
66
67
In order to get the desired behavior, it is necessary to create a ** copy** of
67
68
the value of ` i ` .
68
69
69
70
### Avoiding the reference problem
70
71
71
- In order to copy the value of the loop's index variable, it is best to use an
72
+ In order to copy the value of the loop its index variable, it is best to use an
72
73
[ anonymous wrapper] ( #scopes ) .
73
74
74
75
for(var i = 0; i < 10; i++) {
@@ -84,10 +85,11 @@ argument, therefore it will receive a copy of the **value** of `i` as its
84
85
parameter ` e ` .
85
86
86
87
The anonymous function that gets passed to ` setTimeout ` now has a reference to
87
- ` e ` , which value does not get changed by the loop.
88
+ ` e ` , which value does ** not** get changed by the loop.
88
89
89
- There's another possibility, which is to return a function from the anonymous
90
- wrapper.
90
+ There is another possible way of achieving this. It is possible to return a
91
+ function from the anonymous wrapper, which will then have the same result as the
92
+ code above.
91
93
92
94
for(var i = 0; i < 10; i++) {
93
95
setTimeout((function(e) {
@@ -99,6 +101,6 @@ wrapper.
99
101
100
102
### Best practices
101
103
102
- It is important to understand ** and** master closures, they are the most powerful feature of
103
- JavaScript and are use pretty much everywhere in the language.
104
+ It is important to understand ** and** master closures, they are the most powerful
105
+ feature of JavaScript and are use pretty much everywhere in the language.
104
106
0 commit comments