Iterator :

返回的结果是:{value, done}

function chef(foods){

let i = 0;

return {

next(){

let done = ( i> foods.length);

           let value = !done? foods[i++]:undefined

return{

value,

done

}

}

}

}

let wanghao = chef(['西红柿','鸡蛋'])

console.log(wanghao.next());

console.log(wanghao.next());

...

generator(生成器):

function* chef()

{

yeild '西红柿';

yeild '鸡蛋';

}

let wanghao = chef();

改造下:

function* chef(foods){

for(var i =0;i<foods.length;i++)

{

yeild foods[i]

}

}

var wanghao = chef(['西红柿','鸡蛋'])

console.log(wanghao.next());

console.log(wanghao.next());

...

用class生成的Iterator:

class chef {
constructor(arr) {
this.foods = arr
this.i = 0;
}
next() {
let done = (this.i >= this.foods.length)
let value = !done ? this.foods[this.i++] : undefined
return {
value,
done
}
}
}
var a = new chef([1, 2, 3]);
console.log(a.next());
console.log(a.next());
console.log(a.next());
console.log(a.next());
console.log(a.next());

最新文章

  1. unity导出工程导入到iOS原生工程中详细步骤
  2. android开发虚拟机不能正常启动
  3. supersr--NSURLSessionConfiguration-下载进度
  4. requirejs 小结
  5. Android视图SurfaceView的实现原理分析
  6. JavaScript toFixed() 方法
  7. C# ACM poj1007
  8. 简洁 Abstract Factory模式(3.1)
  9. Using GUID to generate the unique file name in C#
  10. ffmpeg常用参数一览
  11. ASP.NET MVC 学习之路-5
  12. Git远程仓库的使用(三)
  13. win10 uwp 使用 Geometry resources 在 xaml
  14. vue中$on与$emit的实际应用
  15. vmware上虚拟机:Network error: Connection refused 排查
  16. SAP事物代码
  17. linux tail -f messages查看控制台失败
  18. 关于component-scan中base-package包含通配符的问题探究
  19. SpringBoot入门(2)
  20. leetcode Ch1-Search

热门文章

  1. MAC中Parallels Desktop windows忘记密码的解决办法
  2. Qt之QImageWriter
  3. [Angular + TsLint] Disable directive selector tslint error
  4. E-UTRA channel bandwidths per operating band (36.101)
  5. django 笔记15 ajax系列
  6. Linux 设置交换分区
  7. 我所理解的monad(1):半群(semigroup)与幺半群(monoid)
  8. javascript实现多线程 Concurrent.Thread.js
  9. [USACO07MAR]每月的费用Monthly Expense
  10. 今日SGU 5.29