ES6学习笔记六(Iterator和for..of)
{ let arr=['hello','world']; let map=arr[Symbol.iterator](); //返回false时继续执行,true停止执行! console.log(map.next()); console.log(map.next()); console.log(map.next()); } { let obj={ start:[1,3,2], end:[7,8,9], [Symbol.iterator](){ 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 //fasle会继续执行 } }else { return { value:arr[index++], done:true //truee会结束 } } } } } } for(let key of obj){ console.log(key); } }