学习笔记:ES6之Iterator接口和for…of循环

来源:互联网 发布:矩阵的几为3和秩的关系 编辑:程序博客网 时间:2024/06/05 00:55

(1)什么是Iterator接口

(2)Iterator的基本用法

letarr=['hello','world'];

letmap=arr[Symbol.iterator]();

console.log(map.next());

console.log(map.next());

console.log(map.next());

 

(2)部署接口有颜色字体标志的是必须要有的(重要)

letobj={

start:[1,3,2],

end:[7,9,8],

[Symbol.iterator](){

letself=this;

letindex=0;

letarr=self.start.concat(self.end);

letlen=arr.length;

return{

next(){

if(index<len){

return{

value:arr[index++],

done:false

}

}else{

return{

value:arr[index++],

done:true

}

}

}

}

}

};


for(letkeyofobj){

console.log(key);//1 3 2 7 9 8

}

 

for… of

letarr=['hello','world'];

for(letvalueofarr){

console.log(value);//hello world

}

原创粉丝点击