当前位置:网站首页>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
- 38岁女儿不恋爱没有稳定工作老母亲愁哭
- 第15章 泛型
- 周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
- 转转反爬攻防战
- 练习16-两道模拟题
- Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
- Facebook自动化数据分析方案,广告投放省心省力
- Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
- 后管实现面包屑功能
猜你喜欢

Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning

DVWA Clearance Log 2 - Command Injection

Using the TCP protocol, will there be no packet loss?

李航《统计学习方法》笔记之朴素贝叶斯法

The realization of the list

超赞!发现一个APP逆向神器!

Facebook自动化数据分析方案,广告投放省心省力

神通数据库,批量插入数据的时候失败

一文带你了解推荐系统常用模型及框架

全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
随机推荐
armv7与armv8的区别(v8和w12的区别)
Verilog的随机数系统任务----$random
用了TCP协议,就一定不会丢包嘛?
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
未知内容监控
软件测试H模型
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
零代码工具推荐---HiFlow
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
Implementation of mysql connection pool
wireshark的安装教程(暖气片安装方法图解)
Use compilation to realize special effects of love
Spearman's correlation coefficient
身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]
Linux系统卸载,安装,升级,迁移clickHouse数据库
新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!
QT专题:组合会话框和文本编辑器
李航《统计学习方法》笔记之朴素贝叶斯法
练习-17