forked from nkronlage/JavaScripture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.jsdoc
More file actions
75 lines (59 loc) · 1.96 KB
/
iterator.jsdoc
File metadata and controls
75 lines (59 loc) · 1.96 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
70
71
72
73
74
75
Iterator : Object
An Iterator is an Object that returns a sequence of values.
You can use the **for(var value of iterator) {}** to easily loop
over the values in an iterator.
Calling an ECMAScript 2015 generator function (**function*() {}**) return
an Iterator.
You may create your own %%/Iterable|Iterable%% object by assigning the
%%/Symbol#iterator|**Symbol.iterator**%% property to an
object with a **next()** method.
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-iterator-interface
----
instance.next([yieldValue : Object]) : { value : Object, done : Boolean }
Returns an object containing the next value in the iterator.
If all items have been returned, **done** will be **true**.
For generator functions, **yieldValue** is returned to the generator
from the **yield** statement.
<example>
// Generator functions create iterators when called
var generator = function*() {
yield 'foo';
var yieldValue = yield 'bar';
console.log('generator yieldValue = ' + yieldValue);
console.log();
};
// Usually you will iterate over it using for (... of ...)
// which will call next() for you automatically
for (var item of generator()) {
console.log(item);
}
// You can also get the iterator and manually call next()
var iterator = generator();
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next('passed to next'));
// You can create iterable objects manually
var myIterable = { };
myIterable[Symbol.iterator] = function() {
var count = 0;
return {
next: function() {
if (count >= 2) return { value: undefined, done: true };
count++;
return { value: 'my item' + count, done: false };
}
}
};
// Using for (... of ...) calls the next() defined above
for (var item of myIterable) {
console.log(item);
}
// You can also call it manually
iterator = myIterable[Symbol.iterator]();
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
</example>