const iteraterMaker = () => {
let maxIndex = 5; let index = 0; return {
next: function () {
let returnValue = index > 5 ? { done: true } : { value: index, done: false }
index++;
return returnValue;
}
}
}
const myIterater = iteraterMaker();
let value0 = myIterater.next();
console.log(value0) // {"value":0,"done":false}
value0 = myIterater.next();
console.log(value0) // {"value":1,"done":false}
value0 = myIterater.next();
console.log(value0) // {"value":2,"done":false}
value0 = myIterater.next();
console.log(value0) // {"value":3,"done":false}
value0 = myIterater.next();
console.log(value0) // {"value":4,"done":false}
value0 = myIterater.next();
console.log(value0) // {"value":5,"done":false}
value0 = myIterater.next();
console.log(value0) // {"done":true}
value0 = myIterater.next();
console.log(value0) // {"done":true}
console.log("----------分割线-----------")
const yieldFunc = function* () {
yield 1;
yield 2;
yield 3;
}
const yieldFuncIterator = yieldFunc();
console.log(yieldFuncIterator.next()); // {"value":1,"done":false}
console.log(yieldFuncIterator.next()); // {"value":2,"done":false}
console.log(yieldFuncIterator.next()); // {"value":3,"done":false}
console.log(yieldFuncIterator.next()); // {"done":true}
console.log(yieldFuncIterator.next()); // {"done":true}
console.log("----------分割线-----------")
console.log(yieldFuncIterator);
console.log(yieldFuncIterator[Symbol.iterator]()===yieldFuncIterator) //true
console