Can there be generator getters in classes?

前端 未结 1 1100
难免孤独
难免孤独 2020-12-19 03:54

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

class a {
    get *count() {
        let i = 10;
        while(--i) yield i;         


        
相关标签:
1条回答
  • 2020-12-19 04:36

    There is no shorthand notation for this. You can however return a generator from a getter property without any difference:

    function* countdown(i) {
        while(--i) yield i;
    }
    class a {
        get count() {
            return countdown(10);
        }
    }
    

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

    0 讨论(0)
提交回复
热议问题