I mean getters that are generators. All this is ES6+ I believe. Like this maybe.
JavaScript
x
11
11
1
class a {
2
get *count() {
3
let i = 10;
4
while(--i) yield i;
5
}
6
}
7
8
let b = new a;
9
for(const i of b.count)
10
console.log(i);
11
That doesn’t work through, I am placing the asterisk wrong (that is if this is possible at all)
unexpected identifier *
Advertisement
Answer
There is no shorthand notation for this. You can however return a generator from a getter property without any difference:
JavaScript
1
9
1
function* countdown(i) {
2
while(--i) yield i;
3
}
4
class a {
5
get count() {
6
return countdown(10);
7
}
8
}
9
I would recommend to avoid this though. Getters that return distinct stateful objects on consecutive calls can be quite confusing.