let list =[10,20,30];let str ='你好哦';let map =newMap();
map.set('JS','JavaScript');
map.set('P','PHP');
map.set('PY','Python');for(let val of list){
console.log(val);}for(let val of str){
console.log(val);}for(let[key, value]of map){
console.log(key, value);}
let item = map.value();let temp;while(temp = item.next()){if(temp.done){break;}else{
console.log(temp);}}
可迭代的对象
制作一个可迭代的对象
Symbol.iterator
classPlayer{constructor(list){this.list = list;}[Symbol.iterator](){let current =0;let that =this;return{next(){return current < that.list.length ?{value: that.list[current++], done:false}:{ done:true};}}}}let player =newPlayer(['fm','fj','cu']);for(let temp of player){
console.log(temp);}
简单迭代生成器
function* {} 迭代生成器
yield:迭代返回
function*generator(){yield'f';yield'm';yield'j';yield'i';yield'e';}for(let val ofgenerator()){
console.log(val);}
classMyList{constructor(list){this.list = list;this[Symbol.iterator]=function*(){let current =0;let that =this;while(current < that.list.length){yield that.list[current++];}}}}let mylist =newMyList([10,20,30]);for(let val of mylist){
console.log(val);}