You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/06-iterable/article.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@
3
3
4
4
*Iterable* objects is a generalization of arrays. That's a concept that allows to make any object useable in a `for..of` loop.
5
5
6
-
Of course, Arrays are iterable. But there are many other built-in objects, that are iterable as well. For instance, Strings are iterable also. As we'll see, many built-in operators and methods rely on them.
6
+
Of course, Arrays are iterable. But there are many other built-in objects, that are iterable as well. For instance, strings are also iterable.
7
7
8
-
If an object represents a collection (list, set) of something, then `for..of` is a great syntax to loop over it, so let's see how to make it work.
8
+
If an object isn't technically an array, but represents a collection (list, set) of something, then `for..of` is a great syntax to loop over it, so let's see how to make it work.
9
9
10
10
11
11
## Symbol.iterator
@@ -31,9 +31,9 @@ To make the `range` iterable (and thus let `for..of` work) we need to add a meth
31
31
1. When `for..of` starts, it calls that method once (or errors if not found). The method must return an *iterator* -- an object with the method `next`.
32
32
2. Onward, `for..of` works *only with that returned object*.
33
33
3. When `for..of` wants the next value, it calls `next()` on that object.
34
-
4. 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.
34
+
4. The result of `next()` must have the form `{done: Boolean, value: any}`, where `done=true` means that the iteration is finished, otherwise `value`is the next value.
35
35
36
-
Here's the full implementation for `range`:
36
+
Here's the full implementation for `range` with remarks:
37
37
38
38
```js run
39
39
let range = {
@@ -68,10 +68,10 @@ for (let num of range) {
68
68
}
69
69
```
70
70
71
-
Please note the core feature of iterables: an important separation of concerns:
71
+
Please note the core feature of iterables: separation of concerns.
72
72
73
73
- The `range` itself does not have the `next()` method.
74
-
- Instead, another object, a so-called "iterator" is created by the call to `range[Symbol.iterator]()`, and it handles the whole iteration.
74
+
- Instead, another object, a so-called "iterator" is created by the call to `range[Symbol.iterator]()`, and its `next()` generates values for the iteration.
75
75
76
76
So, the iterator object is separate from the object it iterates over.
77
77
@@ -140,9 +140,7 @@ for (let char of str) {
140
140
141
141
## Calling an iterator explicitly
142
142
143
-
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.
144
-
145
-
But to understand things a little bit deeper let's see how to create an iterator explicitly.
143
+
For deeper understanding let's see how to use an iterator explicitly.
146
144
147
145
We'll iterate over a string in exactlly the same way as `for..of`, but with direct calls. This code creates a string iterator and gets values from it "manually":
148
146
@@ -283,7 +281,7 @@ let str = '𝒳😂𩷶';
283
281
284
282
alert( slice(str, 1, 3) ); // 😂𩷶
285
283
286
-
// native method does not support surrogate pairs
284
+
//the native method does not support surrogate pairs
287
285
alert( str.slice(1, 3) ); // garbage (two pieces from different surrogate pairs)
0 commit comments