-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathiterable.jsdoc
More file actions
69 lines (55 loc) · 1.48 KB
/
Copy pathiterable.jsdoc
File metadata and controls
69 lines (55 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Iterable : Object
<p>
An iterable object is any object that has a
%%/Symbol#iterator|**Symbol.iterator**%% property whose value is a
function that returns an %%/Iterator|**Iterator**%%.
</p>
<p>
You can loop over all values in an iterable object by using a
**for (const value of iterable) { }** loop.
</p>
<p>
You may create your own iterable object by assigning the
%%/Symbol#iterator|**Symbol.iterator**%% property to a
generator function (**function* () {}**) or an object with a
%%/Iterator#next|**next()**%% method.
</p>
<p>
See %%/Iterator|Iterator%% for more details.
</p>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-iterable-interface
----
instance[Symbol.iterator] : Function<Iterator>
Returns a function that produces an Iterator for this object.
<example>
// Arrays are a built in Iterable object
const arr = ['a', 'b', 'c'];
// Use for (... of ...) loop to get the values of an iterable
for (const x of arr) {
console.log(x);
}
// Create an iterable class with a generator function
class MyIterable {
constructor(maxValue) {
this._maxValue = maxValue;
}
*[Symbol.iterator]() {
for (let i = 0; i < this._maxValue; i++) {
yield i;
}
}
}
for (const x of new MyIterable(3)) {
console.log(x);
}
// Under the covers, for (... of ...) does the following:
const iterator = arr[Symbol.iterator]();
let current = iterator.next();
while (!current.done) {
console.log(current.value);
current = iterator.next();
}
</example>