当前位置:网站首页>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.
边栏推荐
- One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework
- 3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
- 软件测试与质量 之白盒测试
- 适配器模式适配出栈和队列及优先级队列
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
- Application scenarios of js anti-shake function and function throttling
- 第十七章 Excel操作
- 链表的实现
- Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
- 从零开始Blazor Server(5)--权限验证
猜你喜欢

李航《统计学习方法》笔记之k近邻法

软件测试H模型

HikariCP数据库连接池,太快了!

The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit

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

【技术分享】OSPFv3基本原理

The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes

MySql tens of millions of paging optimization, fast insertion method of tens of millions of data

Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)

You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
随机推荐
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
行为型模式-模板方法模式
众城优选系统开发功能
In the whole development of chi V853 board tried to compile QT test
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
List-based queuing and calling system
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
练习-17
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
Linux system uninstall, install, upgrade, migrate clickHouse database
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
软件工程国考总结——选择题
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
软件测试X模型
用正向迭代器封装实现反向迭代器
ConvNeXt论文及实现
Two-dimensional array piecemeal knowledge sorting
sqlmap安装教程用w+r打开(sqlyog安装步骤)
Event 对象,你很了解吗?