迭代器

1迭代器----帮助我们对某个数据结构进行遍历的对象---有

简单实现可以对数组进行遍历的对象:

 1 const names = ['abc', 'cba', 'nba']
 2     //创建一个迭代器对象来访问数组
 3 let index = 0
 4 const namesIterator = {
 5     next: function() {
 6         if (index < names.length) {
 7             return { done: false, value: names[index++] }
 8         } else {
 9             return { done: true, value: undefined }
10         }
11     }
12 }
13 console.log(namesIterator.next());
14 console.log(namesIterator.next());
15 console.log(namesIterator.next());
16 console.log(namesIterator.next());
17 console.log(namesIterator.next());
18 console.log(namesIterator.next());

 

 2:上面已经实现了一个简单的数组迭代器,但是如果我们想让其他数组生成迭代器呢,我们可以构造一个生成迭代器的函数:

 1 function creatrArrIterator(arr) {
 2     let index = 0
 3     return {
 4         next: function() {
 5             if (index < arr.length) {
 6                 return { done: false, value: arr[index++] }
 7             } else {
 8                 return { done: true, value: undefined }
 9             }
10         }
11     }
12 }
13 const names = ['tyy', 'tsf', 'zjj']
14 const nums = [13, 14, 520]
15 const namesIterator = creatrArrIterator(names)
16 const numsIterator = creatrArrIterator(nums)
17 console.log(namesIterator.next());
18 console.log(namesIterator.next());
19 console.log(namesIterator.next());
20 console.log(namesIterator.next());
21 console.log(numsIterator.next());
22 console.log(numsIterator.next());
23 console.log(numsIterator.next());
24 console.log(numsIterator.next());
25 //创建无限的迭代器 done一直为false
26 function creatNumberItertor() {
27     let index = 0
28     return {
29         next: function() {
30             return { done: false, value: index++ }
31         }
32     }
33 }
34 const NumberItertor = creatNumberItertor()
35 console.log(NumberItertor.next());
36 console.log(NumberItertor.next());
37 console.log(NumberItertor.next());
38 console.log(NumberItertor.next());
39 console.log(NumberItertor.next());
40 console.log(NumberItertor.next());

 

3:可迭代对象---我们获取一个数组时,需要自己创建索引,再创建一个迭代器对象,但是当我们对这些一起进行封装就变成了一个可迭代对象(注意与迭代器区分) 

 1 const iterableObj = {
 2         names: ['abc', 'cba', 'nba'],
 3         //返回一个迭代器
 4         [Symbol.iterator]: function() {
 5             //迭代器访问的是外层作用域的index所以++操作没有问题
 6             let index = 0
 7             console.log(this);
 8             return {
 9                 //变成箭头函数this就去找上层作用域的this
10                 next: () => {
11                     if (index < this.names.length) {
12                         //一个对象不是作用域 nams在外层作用域找不到全局作用域也找不到
13                         return { done: false, value: this.names[index++] }
14                     } else {
15                         return { done: true, value: undefined }
16                     }
17                 }
18             }
19 
20         }
21     }
22     //每次都会生成一个新的迭代器
23 console.log(iterableObj[Symbol.iterator]);
24 const iterator = iterableObj[Symbol.iterator]()
25 console.log(iterator.next());
26 console.log(iterator.next());
27 console.log(iterator.next());
28 console.log(iterator.next());
29 //for...of可以遍历的东西必须是一个可迭代对象 for..in的本质
30 for (const item of iterableObj) {
31     console.log(item);
32 }

4:原生迭代器对象---本身就是一个课可迭代对象--String,Array,Map/Set等等

 1 const iterableObj = {
 2         names: ["abc", "cba", "nba"],
 3         [Symbol.iterator]: function() {
 4             let index = 0
 5             return {
 6                 next: () => {
 7                     if (index < this.names.length) {
 8                         return { done: false, value: this.names[index++] }
 9                     } else {
10                         return { done: true, value: undefined }
11                     }
12                 }
13             }
14         }
15     }
16     //String
17 const str = "I am happy"
18 for (const s of str) {
19     console.log(s);
20 }
21 //Array
22 const arr = ['tyy', 'zjj']
23 for (const a of arr) {
24     console.log(a);
25 }
26 const set1 = Array.from(str)
27 for (const s1 of set1) {
28     console.log(s1);
29 }
30 //Map/Set
31 const map = new Map([
32     [{ name: 'obj1' }, "aaa"],
33     [{ name: 'obj2' }, "bbb"],
34     [1, "ccc"]
35 ])
36 for (const m of map) {
37     console.log(m);
38 }
39 const set = new Set(iterableObj)
40 for (const s of set) {
41     console.log(s);
42 }
43 //arguments对象
44 function foo(x, y, z) {
45     for (const arg of arguments) {
46         console.log(arg);
47     }
48 }
49 foo(10, 20, 30)

5:可迭代对象的应用

 1 //1:for..of场景
 2 //2:展开语法
 3 const iterableObj = {
 4     names: ['tyy', 'tsf', 'zjj'],
 5     [Symbol.iterator]: function() {
 6         let index = 0
 7         return {
 8             next: () => {
 9                 if (index < this.names.length) {
10                     return { done: false, value: this.names[index++] }
11                 } else {
12                     return { done: true, value: undefined }
13                 }
14             }
15         }
16     }
17 
18 
19 }
20 const names = ['tyy', 'tsf', 'zjj']
21 const newnames = [...names, ...iterableObj]
22     //3:解构赋值
23 const obj = { name: 'tsf', age: 22 }
24 const newobj = {...obj }
25 console.log(newobj);
26 const [name1, name2] = iterableObj
27 //ES9新增的一个特性
28 const { name, age } = obj
29 console.log(name1);
30 console.log(name);
31 //4:创建一些其他对象
32 const set1 = new Set(iterableObj)
33 const set2 = new Set(names)
34 
35 const arr1 = Array.from(iterableObj)
36 
37 // 5.Promise.all
38 Promise.all(iterableObj).then(res => {
39     console.log(res)
40 })

 

posted @ 2022-04-04 14:53  沁霓  阅读(37)  评论(0)    收藏  举报