TL;DR
I want to modify the prototype of a generator function instance--that is, the object returned from calling a function*
.
Let's say I have a generator function:
function* thing(n){
while(--n>=0) yield n;
}
Then, I make an instance of it:
let four = thing(4);
I want to define a prototype of generators called exhaust
, like so:
four.exhaust(item => console.log(item));
which would produce:
3
2
1
0
I can hack it by doing this:
(function*(){})().constructor.prototype.exhaust = function(callback){
let ret = this.next();
while(!ret.done){
callback(ret.value);
ret = this.next();
}
}
However, (function*(){})().constructor.prototype.exhaust
seems very... hacky. There is no GeneratorFunction
whose prototype I can readily edit... or is there? Is there a better way to do this?