乐趣区

摘自ES6标准入门第3版阮一峰著经典案例持续更新中

1. 模拟 next 方法返回值的例子

function makeIterator(array){
   var nextIndex = 0;
   return {next: function(){return nextIndex < array.length ? {value: array[nextIndex++],done:false} : {value: undefined, done: true};
     }
   }
}
var it = makeIterator(['a','b']);
it.next(); // {value: "a", done: false}
it.next(); // {value: "b", done: true}
it.next(); // {value: undefined, done: true}
退出移动版