Skip to content

Commit 92b9dbd

Browse files
committed
More cleanup
1 parent 25a0e6e commit 92b9dbd

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

doc/closures.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ defined in. Since the only scope that JavaScript has is the
2525
foo.increment();
2626
foo.get(); // 5
2727

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.
3232

3333
### Why private variables work
3434

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
3636
**no** way of accessing the variable `count` from the outside. The only way to
3737
interact with it is via the two closures.
3838

@@ -45,30 +45,31 @@ This code will **not** change the variable `count` in the scope of `Counter`,
4545
since `foo.hack` was not defined in **that** scope, instead, it will create
4646
(or override) the *global* variable `count`.
4747

48-
One common mistake made about closures is that they do **not** copy the
49-
variables of their outer scope, they **reference** them.
50-
5148
### Closures inside loops
5249

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+
5354
for(var i = 0; i < 10; i++) {
5455
setTimeout(function() {
5556
console.log(i);
5657
}, 1000);
5758
}
5859

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.
6162

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
6465
`10`.
6566

6667
In order to get the desired behavior, it is necessary to create a **copy** of
6768
the value of `i`.
6869

6970
### Avoiding the reference problem
7071

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
7273
[anonymous wrapper](#scopes).
7374

7475
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
8485
parameter `e`.
8586

8687
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.
8889

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.
9193

9294
for(var i = 0; i < 10; i++) {
9395
setTimeout((function(e) {
@@ -99,6 +101,6 @@ wrapper.
99101

100102
### Best practices
101103

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.
104106

0 commit comments

Comments
 (0)