014-Iterator接口和for...of循环
2018-03-26 22:59
316 查看
/** * Iterator接口和for...of循环 * * 介绍: * 如Array Object Set Map等数据结构,的使用方式各异,而通过Iterator接口 * 致力于统一他们的调用方式,让他们在使用上尽可能的相同,这就是他的功能 * */ { //数组内置的Iterator接口 let arr=['hello','world']; let map=arr[Symbol.iterator](); console.log(map.next()); console.log(map.next()); console.log(map.next()); } { //自定义Iterator接口 let obj={ start:[1,3,2], end:[7,9,8], [Symbol.iterator](){ /** * 达到的效果(先遍历start,再遍历end) * @type {obj} */ let self=this; let index=0; let arr=self.start.concat(self.end); let len=arr.length; return { next(){ if(index<len){ return { value:arr[index++], done:false } }else{ return { value:arr[index++], done:true } } } } } } for(let key of obj){ console.log(key); //1 3 2 7 9 8 } } { //for...of循环 let arr=['hello','world']; for(let value of arr){ console.log('value',value); } }
相关文章推荐
- 学习笔记:ES6之Iterator接口和for…of循环
- Iterator接口和for...of循环
- ES6--iterator和for-of循环
- ECMAScript 6 学习笔记----Iterator和for...of循环
- iterator和for-of循环
- ECMAScript6笔记:Iterator和for...of循环
- ES6学习笔记(七)Iterator和for...of循环
- ES6 之 Set数据结构和Map数据结构 Iterator和for...of循环
- 第十三节,Iterator和for...of循环
- Iterator和for...of循环
- ES6学习笔记之Iterator和for...of循环
- ECMAScript6标准Iterator和for...of循环
- 5、Iterator和for...of循环—ES6学习笔记
- 迭代器(iterator)和for...of循环
- 15.Iterator和for...of循环
- ES6入门教程之Iterator与for...of循环详解
- 集合框架(三)使用Iterator接口来遍历一个集合和使用for-each循环简化
- 自学-ES6篇-Iterator和for...of循环
- es6 iterator和for...of循环