Skip to content

Commit db18204

Browse files
committed
minor
1 parent d391eb1 commit db18204

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

1-js/05-data-types/06-iterable/article.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Arrays by themselves are iterable. But not only arrays. Strings are iterable too, and many other built-in objects as well.
77

8-
Iterables are widely used by the core JavaScript, as we'll see many operators and built-in methods rely on them.
8+
Iterables are widely used by the core JavaScript. As we'll see many built-in operators and methods rely on them.
99

1010
[cut]
1111

@@ -23,13 +23,13 @@ let range = {
2323
to: 5
2424
};
2525

26-
// We want for..of to work:
26+
// We want the for..of to work:
2727
// for(let num of range) ... num=1,2,3,4,5
2828
```
2929

3030
To make the `range` iterable (and thus let `for..of` work) we need to add a method to the object named `Symbol.iterator` (a special built-in symbol just for that).
3131

32-
- When `for..of` starts, it calls that method (or errors if none found).
32+
- When `for..of` starts, it calls that method (or errors if not found).
3333
- The method must return an *iterator* -- an object with the method `next`.
3434
- When `for..of` wants the next value, it calls `next()` on that object.
3535
- The result of `next()` must have the form `{done: Boolean, value: any}`, where `done=true` means that the iteration is finished, otherwise `value` must be the new value.
@@ -47,13 +47,13 @@ range[Symbol.iterator] = function() {
4747

4848
// 2. ...it returns the iterator:
4949
return {
50-
current: this.from, // start at "range.from",
51-
last: this.to, // end at "range.to"
50+
current: this.from,
51+
last: this.to,
5252

53-
// 3. next() is called on each iteration by for..of
53+
// 3. next() is called on each iteration by the for..of loop
5454
next() {
55+
// 4. it should return the value as an object {done:.., value :...}
5556
if (this.current <= this.last) {
56-
// 4. it should return the value as an object {done:.., value :...}
5757
return { done: false, value: this.current++ };
5858
} else {
5959
return { done: true };
@@ -62,6 +62,7 @@ range[Symbol.iterator] = function() {
6262
};
6363
};
6464

65+
// now it works!
6566
for (let num of range) {
6667
alert(num); // 1, then 2, 3, 4, 5
6768
}
@@ -72,9 +73,9 @@ There is an important separation of concerns in this code:
7273
- The `range` itself does not have the `next()` method.
7374
- Instead, another object, a so-called "iterator" is created by the call to `range[Symbol.iterator]()`, and it handles the iteration.
7475

75-
So, the iterator is separate from the object.
76+
So, the iterator object is separate from the object it iterates over.
7677

77-
Technically, we may merge them and use `range` itself as the iterator, to make the code simpler.
78+
Technically, we may merge them and use `range` itself as the iterator to make the code simpler.
7879

7980
Like this:
8081

@@ -102,7 +103,7 @@ for (let num of range) {
102103
}
103104
```
104105

105-
Now `range[Symbol.iterator]()` returns the `range` object itself, and it has the necessary `next()` method. Sometimes that's fine too. The downside is that now it's impossible to have two `for..of` loops running over the object simultaneously: they'll share the iteration state, because there's only one iterator -- the object itself.
106+
Now `range[Symbol.iterator]()` returns the `range` object itself: it has the necessary `next()` method and remembers the current iteration progress in `this.current`. Sometimes that's fine too. The downside is that now it's impossible to have two `for..of` loops running over the object simultaneously: they'll share the iteration state, because there's only one iterator -- the object itself.
106107

107108
```smart header="Infinite iterators"
108109
Infinite iterators are also doable. For instance, the `range` becomes infinite for `range.to = Infinity`. Or we can make an iterable object that generates an infinite sequence of pseudorandom numbers. Also can be useful.
@@ -138,9 +139,9 @@ for(let char of str) {
138139

139140
Normally, internals of iterables are hidden from the external code. There's a `for..of` loop, that works, that's all it needs to know.
140141

141-
But to understand things a little bit more deeper let's see how to create an iterator explicitly. We'll do that the same way as `for..of`, but with direct calls.
142+
But to understand things a little bit more deeper let's see how to create an iterator explicitly.
142143

143-
For instance, this code gets a string iterator and calls it "manually":
144+
We'll iterate over a string the same way as `for..of`, but with direct calls. This code gets a string iterator and calls it "manually":
144145

145146
```js run
146147
let str = "Hello";
@@ -157,16 +158,16 @@ while(true) {
157158
}
158159
```
159160

160-
That is rarely needed, but gives us more control than `for..of`. For instance, we can split the iteration process: iterate a bit, then stop, do something else, and then resume later.
161+
That is rarely needed, but gives us more control over the process than `for..of`. For instance, we can split the iteration process: iterate a bit, then stop, do something else, and then resume later.
161162

162163
## Iterables and array-likes [#array-like]
163164

164-
There are two official terms that look similar, but are very different. Please be careful to avoid the confusion.
165+
There are two official terms that look similar, but are very different. Please make sure you understand them well to avoid the confusion.
165166

166167
- *Iterables* are objects that implement the `Symbol.iterator` method, as described above.
167168
- *Array-likes* are objects that have indexes and `length`, so they look like arrays.
168169

169-
Naturally, they can combine. For instance, strings are both iterable and array-like.
170+
Naturally, these properties can combine. For instance, strings are both iterable (`for..of` works on them) and array-like (they have numeric indexes and `length`).
170171

171172
But an iterable may be not array-like and vice versa.
172173

@@ -236,7 +237,7 @@ let arr = Array.from(range, num => num * num);
236237
alert(arr); // 1,4,9,16,25
237238
```
238239

239-
We can also use `Array.from` to turn a string into an array of characters:
240+
Here we use `Array.from` to turn a string into an array of characters:
240241

241242
```js run
242243
let str = '𝒳😂';

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ Here's what's going on in the `makeCounter` example step-by-step, follow it to m
334334

335335
3. During the execution of `makeCounter()`, a tiny nested function is created.
336336

337-
It doesn't matter whether the function is created using Function Declaration or Function Expression. All functions get the `[[Environment]]` property that references the Lexical Environment where they were made.
337+
It doesn't matter whether the function is created using Function Declaration or Function Expression. All functions get the `[[Environment]]` property that references the Lexical Environment where they were made. So that new tiny nested function gets it as well.
338338

339-
For our new nested function that is the current Lexical Environment of `makeCounter()`:
339+
For our new nested function the value of `[[Environment]]` is the current Lexical Environment of `makeCounter()` (where it was born):
340340

341341
![](lexenv-nested-makecounter-3.png)
342342

0 commit comments

Comments
 (0)