Skip to content
Advertisement

Can there be generator getters in classes?

I mean getters that are generators. All this is ES6+ I believe. Like this maybe.

JavaScript

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

I would recommend to avoid this though. Getters that return distinct stateful objects on consecutive calls can be quite confusing.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement