我正在学习ES6中的生成器,想要从概念上理解下面这个函数中发生了什么:
function* createNames() {
const people = [];
people.push(yield);
people.push(yield);
people.push(yield);
return people;
}
const iterator = createNames();
iterator.next('Brian');
iterator.next('Paul');
iterator.next('John');
iterator.next(); // output: ["Paul", "John", undefined]
我的问题是:为什么第一次推送被忽略了?数组不应该像
people = ['Brian', 'John', 'Paul', undefined]
这样吗?抱歉问一个愚蠢的问题,但我真的很想完全理解这个。提前谢谢!