当前位置:网站首页>LayaBox---TypeScript---Iterator and generator
LayaBox---TypeScript---Iterator and generator
2022-08-02 10:11:00 【Gragra】
1. Iterability
When an object implements the Symbol.iterator property, we consider it to be iterable.Some built-in types such as Array, Map, Set, String, Int32Array, Uint32Array, etc. have all implemented their own Symbol.iterator.The Symbol.iterator function on the object is responsible for returning the value for iteration.
1.1 for..of statement
let someArray = [1, "string", false];for (let entry of someArray) {console.log(entry); // 1, "string", false}1.2 for..of vs. for..in statement
for..of and for..in both iterate over a list; but the values used for iteration are different,
Difference 1:for..in iterates over the object's key list, while for..of iterates over the object's key corresponding value.let list = [4, 5, 6];for (let i in list) {console.log(i); // "0", "1", "2",}for (let i of list) {console.log(i); // "4", "5", "6"}Difference two:for..in can operate on any object and provides a way to view the properties of the object.for..of is concerned with iterating over the values of the object.let pets = new Set(["Cat", "Dog", "Hamster"]);pets["species"] = "mammals";for (let pet in pets) {console.log(pet); // "species"}for (let pet of pets) {console.log(pet); // "Cat", "Dog", "Hamster"}2. Code generation
When the build target is ES5 or ES3, Iterators are only allowed on Array types.Using the for..of statement on non-array values will get an error, even if those non-array values already implement the Symbol.iterator property.
The compiler will generate a simple for loop as a for..ofloop
let numbers = [1, 2, 3];for (let num of numbers) {console.log(num);}The generated code is:
var numbers = [1, 2, 3];for (var _i = 0; _i < numbers.length; _i++) {var num = numbers[_i];console.log(num);}When the target is an engine compatible with ECMAScipt 2015, the compiler will generate the for..of built-in iterator implementation of the corresponding engine.
边栏推荐
- 李航《统计学习方法》笔记之监督学习Supervised learning
- 行为型模式-模板方法模式
- Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
- ConvNeXt论文及实现
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
- 阿里巴巴 CTO 程立:开源是基础软件的源头!
- iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
- 图形化矩阵,矩阵到底长什么样?
- 练习16-两道模拟题
- The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
猜你喜欢
随机推荐
迭代器失效问题
Linux system uninstall, install, upgrade, migrate clickHouse database
练习16-两道模拟题
链表的实现
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
用了TCP协议,就一定不会丢包嘛?
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
DVWA 通关记录 2 - 命令注入 Command Injection
李航《统计学习方法》笔记之感知机perceptron
未知内容监控
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
STL中list实现
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
armv7与armv8的区别(v8和w12的区别)
阿里CTO程立:阿里巴巴开源的历程、理念和实践
练习-17
零代码工具推荐---HiFlow
2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
行为型模式-策略模式









