1 //iterator  for ...of循环
 2 {
 3     let arr=['hello','world'];
 4     let map=arr[Symbol.iterator]();
 5     console.log(map.next())
 6     console.log(map.next())
 7     console.log(map.next())
 8 }
 9 {
10     let obj={
11         start:[1,3,2],
12         end:[7,9,8],
13         [Symbol.iterator](){
14             let self=this;
15             let index=0;
16             let arr=self.start.concat(self.end);
17             let len=arr.length;
18             return {
19                 next(){
20                     if(index<len){
21                         return {
22                             value:arr[index++],
23                             done:false
24                         }
25                     }else{
26                         return {
27                             value:arr[index++],
28                             done:true
29                         }
30                     }
31                 }
32             }
33         }
34     }
35     for(let key of obj){
36         console.log(key)
37     }
38 }
39 {
40     let arr=['hello','world'];
41     for(let value of arr){
42         console.log('value',value)
43     }
44 }

 

posted on 2018-06-26 10:32  chenlw101  阅读(102)  评论(0编辑  收藏  举报