课堂上可以有发生器吸气剂吗? [英] Can there be generator getters in classes?

查看:136
本文介绍了课堂上可以有发生器吸气剂吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是作为发电机的吸气剂。这一切都是ES6 +我相信。也许这样。

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;
    }
}

let b = new a;
for(const i of b.count)
    console.log(i);

这不起作用,我把星号放错了(如果这是可能的话所有)

That doesn't work through, I am placing the asterisk wrong (that is if this is possible at all)


意外标识符*

unexpected identifier *


推荐答案

此处没有简写符号。但是,您可以从getter属性返回一个生成器而没有任何区别:

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);
    }
}

我建议尽量避免这种情况。在连续调用中返回不同状态对象的getter可能会非常混乱。

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

这篇关于课堂上可以有发生器吸气剂吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆