当前位置:网站首页>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.
边栏推荐
猜你喜欢

Getting Started with SCM from Scratch (1): Summary of Background Knowledge

Implementation of mysql connection pool

向量点积(Dot Product),向量叉积(Cross Product)

leetcode 62. Unique Paths(独特的路径)

后管实现面包屑功能

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

关于缓存数据的探讨

In the whole development of chi V853 board tried to compile QT test

WPF 截图控件之文字(七)「仿微信」

MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
随机推荐
matlab-day02
Rust 从入门到精通03-helloworld
使用较广泛的安全测试工具有哪些?
链表的实现
零代码工具推荐---HiFlow
用了TCP协议,就一定不会丢包嘛?
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
R语言使用zoo包中的rollapply函数以滚动的方式、窗口移动的方式将指定函数应用于时间序列、设置align参数指定结果数据中的时间标签取自窗口中的位置(参数right指定取自窗口的最右侧)
HikariCP数据库连接池,太快了!
Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
程序员的浪漫七夕
Verilog的随机数系统任务----$random
Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
适配器模式适配出栈和队列及优先级队列
Pytorch's LSTM parameters explained
练习16-两道模拟题
Use the scrapy to climb to save data to mysql to prevent repetition