blog/iterables #2
Closed
Replies: 2 comments
-
Test comment |
Beta Was this translation helpful? Give feedback.
0 replies
-
const range = {
from: 1,
to: 10,
};
range[Symbol.iterator] = function* () {
let start = this.from;
let end = this.to;
while (start <= end) {
yield start;
start++;
}
};
// still works the same for..of loop
for (let num of range) {
console.log(num); // 1 2 3 4 5
}
// explicit iterator
const generator = range[Symbol.iterator]();
generator.next(); // { "value": 1, "done": false } |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
blog/iterables
Iterable objects are a generalization of arrays. That’s a concept that allows us to make any object useable in a for..of loop.
http://localhost:3000/blog/iterables
Beta Was this translation helpful? Give feedback.
All reactions