Tuesday, 4 April 2017

Reference GeneratorFunction instance from within the function definition

I would like to set properties on the instantiated GeneratorFunction. I want to be able to set the length property as a convenience, so the consumer can get an idea of how many values will be generated. For example:

function* produceValues(someInput) {
    this.length = determineLength(someInput)
    yield // something
}

const gen = produceValues(input)
console.log(gen.length)

Unsurprisingly, using this does not refer to the instance, but instead the global. Does JavaScript provide some way to access the instantiated object?



via Andy Carlson

No comments:

Post a Comment