var myIterator = {}
//Generator函数就是一个遍历器对象的生成函数
myIterator[Symbol.iterator] = function* () {
yield 1;
yield 2;
yield 3;
}
for (let v of myIterator) {
console.log(v)
}
function* gen() {
}
//通过Generator函数执行后返回遍历器对象
let g = gen()
//遍历器对象本身也具有Symbol.iterator
console.log(g[Symbol.iterator]() === g)
function* foo() {
yield 1;
yield 2;
yield 3;
yield 4;
return 5;
}
for (let v of foo()) {
console.log(v)
}
原文:https://www.cnblogs.com/lceihen/p/14454089.html