Reputation: 29129
For a ES2015 js class I would like to have an iterator. So I tried the following
export default class MyList {
iterator* () {
// Dummy implementation
let counter = 0;
while(true) {
yield counter++;
}
}
}
Because I would like to do
let list = new MyList();
iter = list.iterator();
while(!iter.done()) { ... }
For example.
However, this doesn't work. So the question is, what is the preferred way to implement an iterator inside a class ?
Upvotes: 1
Views: 543
Reputation: 1451
The '*' should be before function name. Like this:
export default class MyList {
*iterator() {
// Dummy implementation
let counter = 0;
while(true) {
yield counter++;
}
}
}
You're welcome.
Upvotes: 5