ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

生成器替代迭代器的使用

2022-04-05 15:31:21  阅读:157  来源: 互联网

标签:index console log 迭代 生成器 yield next done 替代


生成器替代迭代器会让代码更加的简洁----主要是是运用 “yield 值”----对应于{done:false,value:值} 同时就可以借助遍历来让代码更加的简洁 具体看代码

yield*---表达式用于委托另一个generator或者可迭代对象

 1 //1:生成器来替代迭代器
 2 function* creatrArrIterator(arr) {
 3     //4:第四种写法
 4     // yield "tsf" //{done:false,value:"tsf"}
 5     // yield "tyy" //{done:false,value:"tyy"}
 6     // yield "zjj" //{done:false,value:"zjj"}
 7     //3:第三种写法 后面跟上可迭代对象
 8     yield* arr
 9     //2:第二种写法
10     // for (const a of arr) {
11     //     yield a
12     // }
13     //1:第一种写法
14     // return {
15     //     next: function() {
16     //         if (index < arr.length) {
17     //             return { done: false, value: arr[index++] }
18 
19     //         } else {
20     //             return { done: true, value: undefined }
21 
22     //         }
23     //     }
24     // }
25 }
26 const names = ['tyy', "tsf", "zjj"]
27 const namesIterator = creatrArrIterator(names)
28 console.log(namesIterator.next());
29 console.log(namesIterator.next());
30 console.log(namesIterator.next());
31 console.log(namesIterator.next());
32 //2:创建一个函数,这个函数可以迭代一个范围内的数字
33 //10 20
34 function* createRangeIterator(start, end) {
35     let index = start
36         //1:第一种写法
37         // return {
38         //     next: function() {
39         //         if (index < end) {
40         //             return { done: false, value: index++ }
41         //         } else {
42         //             return { done: true, value: undefined }
43         //         }
44         //     }
45         // }
46     while (index < end) {
47         yield index++
48 
49     }
50 }
51 const RangeIterator = createRangeIterator(10, 20)
52 console.log(RangeIterator.next());
53 console.log(RangeIterator.next());
54 console.log(RangeIterator.next());
55 console.log(RangeIterator.next());
56 console.log(RangeIterator.next());
57 //3:ClassRoom案列
58 class Classroom {
59     constructor(address, name, students) {
60         this.address = address
61         this.name = name,
62             this.students = students
63     }
64     entry(newstudent) {
65             this.students.push(newstudent)
66         }
67         foo
68         //3:另一种语法
69         // *[Symbol.iterator]() {
70         //     yield* this.students
71         // }
72         //2:第二种写法
73         [Symbol.iterator] = function*() {
74             yield* this.students
75         }
76         //1:第一种写法
77         // [Symbol.iterator]() {
78         //     let index = 0
79         //     return {
80         //         next: () => {
81         //             if (index < this.students.length) {
82         //                 return { done: false, value: this.students[index++] }
83         //             } else {
84         //                 return { done: true, value: undefined }
85         //             }
86         //         },
87         //     }
88         // }
89 }
90 const calssroom = new Classroom("1栋", "实验楼", ['tyy', 'tsf'])
91 calssroom.entry('zjj')
92 for (const s of calssroom) {
93     console.log(s);
94 }

 

标签:index,console,log,迭代,生成器,yield,next,done,替代
来源: https://www.cnblogs.com/tyysf/p/16102501.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有