【ES6】Iterator和Generator
来源:互联网 发布:怎么告别人网络诽谤 编辑:程序博客网 时间:2024/06/05 22:56
前言:
前段时间在整理有关于ES6的文档,一边学习,一边总结。大致列出的一个提纲,自己对ES6的理解,仅供大家参考学习。
ES6规定对象只要具有Symbol.iterator属性,那么此对象就是可遍历的。Symbol.iterator的返回值是一个遍历器,可以通过next()和for…of…遍历。
(()=>{ let arr = ['a','b','c']; let gen = arr[Symbol.iterator]() console.log(gen.next()) console.log(gen.next()) console.log(gen.next()) console.log(gen.next()) console.log(gen.next())})();
也可以使用for…of…遍历出全部内容
用户可以自定生成器,返回一个遍历器对象。
(()=>{ function* he(){ yield 'hello'; yield 'world'; } let a = he(); console.log(a.next()) console.log(a.next()) console.log(a.next())})();
此外,使用Generator可以实现执行同步事件的效果。原理:可以通过yield的返回值判断是否继续执行next( )。
声明:参考自自阮一峰老师《ES6入门》,有兴趣的可以去看下,url:http://es6.ruanyifeng.com/
备注:时间关系,关于ES6的更新不会很快。如有需要完整文档的,可以留下邮箱,单独发送>_<
阅读全文
0 0
- 【ES6】Iterator和Generator
- ES6中的迭代器(Iterator)和生成器(Generator)
- Iterator和Generator
- Iterator 和 Generator的学习心得
- 【es6】Iterator 和 for...of
- ES6--Generator
- ES6-generator
- 【es6】Generator
- ES6 Generator
- ES6 Generator
- ES6-Generator函数和async函数
- python语法31[iterator和generator+yield]
- [es6]Symbol.iterator 和 for of
- ES6--Iterator和for...of循环
- es6 iterator和for...of循环
- ES6 -- 遍历器Iterator和for...of
- ES6 —(Iterator 和 for...of)
- ES6--iterator和for-of循环
- 在SuperMap iClient 9D for MapboxGL中使用ECharts和MapV
- Windows8.1&&10 安装 Tensorflow 遇到的问题
- eclipse搭建安卓环境
- 新浪前端笔试题
- 【软考之旅】第十二章 面向对象
- 【ES6】Iterator和Generator
- WPF编译时出现【程序“\MVC_DHCC.exe”不包含适合于入口点的静态“Main”方法】的错误
- ctags,cscope,nerdtree,tmux 笔记
- 洛谷 P3383【模板】线性筛素数
- java.net.BindException: 地址已在使用
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin
- HDU 1521 排列 指数母函数
- 判断元素出栈顺序是否合法
- 在EasyPlayerPro上实现一键平稳切换视频源的功能