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
+7-7
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
2
2
# Iterables
3
3
4
-
*Iterable* objects is a generalization of arrays. That's a concept that allows us to make any object useable in a `for..of` loop.
4
+
*Iterable* objects are a generalization of arrays. That's a concept that allows us to make any object useable in a `for..of` loop.
5
5
6
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
@@ -26,7 +26,7 @@ let range = {
26
26
// for(let num of range) ... num=1,2,3,4,5
27
27
```
28
28
29
-
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).
29
+
To make the `range`object 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).
30
30
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*.
@@ -140,7 +140,7 @@ for (let char of str) {
140
140
141
141
## Calling an iterator explicitly
142
142
143
-
For deeper understanding let's see how to use an iterator explicitly.
143
+
For deeper understanding, let's see how to use an iterator explicitly.
144
144
145
145
We'll iterate over a string in exactly the same way as `for..of`, but with direct calls. This code creates a string iterator and gets values from it "manually":
146
146
@@ -165,16 +165,16 @@ That is rarely needed, but gives us more control over the process than `for..of`
165
165
166
166
## Iterables and array-likes [#array-like]
167
167
168
-
There are two official terms that look similar, but are very different. Please make sure you understand them well to avoid the confusion.
168
+
Two official terms look similar, but are very different. Please make sure you understand them well to avoid the confusion.
169
169
170
170
-*Iterables* are objects that implement the `Symbol.iterator` method, as described above.
171
171
-*Array-likes* are objects that have indexes and `length`, so they look like arrays.
172
172
173
-
When we use JavaScript for practical tasks in browser or other environments, we may meet objects that are iterables or array-likes, or both.
173
+
When we use JavaScript for practical tasks in a browser or any other environment, we may meet objects that are iterables or array-likes, or both.
174
174
175
175
For instance, strings are both iterable (`for..of` works on them) and array-like (they have numeric indexes and `length`).
176
176
177
-
But an iterable may be not array-like. And vice versa an array-like may be not iterable.
177
+
But an iterable may not be array-like. And vice versa an array-like may not be iterable.
178
178
179
179
For example, the `range` in the example above is iterable, but not array-like, because it does not have indexed properties and `length`.
180
180
@@ -293,7 +293,7 @@ alert( str.slice(1, 3) ); // garbage (two pieces from different surrogate pairs)
293
293
Objects that can be used in `for..of` are called *iterable*.
294
294
295
295
- Technically, iterables must implement the method named `Symbol.iterator`.
296
-
- The result of `obj[Symbol.iterator]()` is called an *iterator*. It handles the further iteration process.
296
+
- The result of `obj[Symbol.iterator]()` is called an *iterator*. It handles further iteration process.
297
297
- An iterator must have the method named `next()` that returns an object `{done: Boolean, value: any}`, here `done:true` denotes the end of the iteration process, otherwise the `value` is the next value.
298
298
- The `Symbol.iterator` method is called automatically by `for..of`, but we also can do it directly.
299
299
- Built-in iterables like strings or arrays, also implement `Symbol.iterator`.
0 commit comments