【发布时间】:2015-08-05 06:47:19
【问题描述】:
如何获得生成器的第 n 个值?
function *index() {
let x = 0;
while(true)
yield x++;
}
// the 1st value
let a = index();
console.log(a.next().value); // 0
// the 3rd value
let b = index();
b.next();
b.next();
console.log(b.next().value); // 2
// the nth value?
let c = index();
let n = 10;
console.log(...); // 9
【问题讨论】:
-
你试过循环吗?
标签: javascript generator ecmascript-6