传递给next()的参数如何处理?

时间:2018-10-31 14:39:58

标签: javascript iterator generator

我不知道为什么iterator.next(4)打印{值:6,完成:false}?为什么将4分配给变量first?其背后的机制是什么?

function *createIterator() {
    let first = yield 1;
    let second = yield first + 2;
    yield second + 3;
}
let iterator = createIterator();
console.log(iterator.next()); // "{ value: 1, done: false }"
console.log(iterator.next(4));// "{ value: 6, done: false }"
console.log(iterator.next(5));// "{ value: 8, done: false }"
console.log(iterator.next());

0 个答案:

没有答案