0
我想寫一個簡單的發電機獲得通過調用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}
爲什麼這臺發電機只是產生2的值增加的價值?
我改變了代碼
function* next_id() {
var current_id = 1;
while (1) {
yield current_id;
current_id ++;
}
return current_id;
}
它的工作原理,這真的讓我感到困惑。
多數民衆贊成在發電機工作正確 –