-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path3-class.js
More file actions
42 lines (36 loc) · 801 Bytes
/
3-class.js
File metadata and controls
42 lines (36 loc) · 801 Bytes
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
'use strict';
class Counter {
constructor(begin, end, step = 1) {
this.begin = begin;
this.end = end;
this.step = step;
}
[Symbol.iterator]() {
const end = this.end;
let i = this.begin;
const step = this.step;
const iterator = {
next() {
const item = {
value: i,
done: i > end
};
i += step;
return item;
}
};
return iterator;
}
}
// Usage
const iterable = new Counter(0, 3);
const iterator = iterable[Symbol.iterator]();
const step1 = iterator.next();
const step2 = iterator.next();
const step3 = iterator.next();
const step4 = iterator.next();
console.log({ step1, step2, step3, step4 });
for (const step of iterable) {
console.log({ step });
}
console.log({ steps: [...iterable] });