-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterators.js
More file actions
58 lines (32 loc) · 793 Bytes
/
Copy pathiterators.js
File metadata and controls
58 lines (32 loc) · 793 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const array = [10, 20, 30];
const iterator = array[Symbol.iterator]();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log("----------------");
// Custom Iterator
const numbers = {
start: 1,
end: 5,
[Symbol.iterator]() {
let current = this.start;
let end = this.end;
return {
next() {
if (current <= end) {
return {
value: current++,
done: false
};
}
return {
done: true
};
}
};
}
};
for (const number of numbers) {
console.log(number);
}