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

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

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

【New Edition】DeepFakes: Creation, Detection and Influence

HikariCP database connection pool, too fast!

Implementation of mysql connection pool

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

链表的实现

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

Application scenarios of js anti-shake function and function throttling

李航《统计学习方法》笔记之k近邻法
随机推荐
matlab-day02
如何封装微信小程序的 wx.request() 请求
wireshark的安装教程(暖气片安装方法图解)
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
QT专题:自定义部件
从零开始入门单片机(一):必会背景知识总结
js防抖函数和函数节流的应用场景
李航《统计学习方法》笔记之监督学习Supervised learning
Pytorch的LSTM参数解释
The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes
R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
【技术分享】OSPFv3基本原理
未知内容监控
李航《统计学习方法》笔记之朴素贝叶斯法
行为型模式-模板方法模式
Facebook自动化数据分析方案,广告投放省心省力
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
8月份的.NET Conf 活动 专注于 .NET MAUI
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars