私はこの発電機はわずか2値を生成するのはなぜコールnext
JavaScript、ジェネレータが失敗した理由
> var gg = next_id();
> function* next_id() {
var current_id = 1;
yield current_id;
current_id ++;
return current_id;
}
> gg.next()
Object {value: 1, done: false}
> gg.next()
Object {value: 2, done: true}
> gg.next()
Object {value: undefined, done: true}
> gg.next()
Object {value: undefined, done: true}
増加した値を取得するには、単純な発電機を書きたいですか?
そして、私はそれが本当に私は混乱して作られた、それが動作するコード
function* next_id() {
var current_id = 1;
while (1) {
yield current_id;
current_id ++;
}
return current_id;
}
を変更しました。
thats発電機の動作方法 –