代码
提交代码
interface IteratorInterface { next: () => { value: any done: boolean } } function createIterator(array: any[]): IteratorInterface { let index = 0 let len = array.length return { next: function () { return index < len ? { value: array[index++], done: false } : { value: undefined, done: true } } } } var iterator = createIterator([1, 2, 3]) console.log(iterator.next()) // { value: 1, done: false } console.log(iterator.next()) // { value: 2, done: false } console.log(iterator.next()) // { value: 3, done: false } console.log(iterator.next()) // { value: undefined, done: true }
运行结果