当前位置:网站首页>ES6 adds -iterator traversal, for..Of loop
ES6 adds -iterator traversal, for..Of loop
2022-07-24 23:11:00 【Cabbage 007】
Catalog
2. Have iterator The data structure of the interface is as follows :
3.iterator Principle of traverser
4.Iterator The execution mechanism of the traverser
for .. of Realization principle
One .interator Iterator
1. effect
- For a variety of data structures , Provide a unified , Convenient access interface
- for for...of consumption
2. Have iterator The data structure of the interface is as follows :
- Array/ An array of class
- String
- Set
- Map
3.iterator Principle of traverser
es6 Regulations , default iterator Interface deployed in data structure Symbol.iterator( Unique data type attributes ) attribute , Or say , As long as a data structure has Symbol,iterator attribute , Namely " Ergodic "
4.Iterator The execution mechanism of the traverser
const arr = [ " The tiger "," The tiger "," raw "," Wei " ]
1: Get the iterator object , It is essentially a pointer object
const iter = arr[Symbol.iterator]()
console,log(iter)

2: call next Method , The pointer points to the first member of the data structure . And so on
say concretely , Is to return a containing value and done Objects with two properties .
among ,value Property is the value of the current member ,done Property is a Boolean value , Indicates whether the traversal ends .
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )

5. To strengthen
const str = "2022"
const iter = str[Symbol.iterator]()
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )
console.log(iter.next() )

const obj = {
a:1, b:2, c:3 }
obj.[Symbol.iterator]()
There is no deployment on the object Symbol.iterator attribute
One .for...of loop
A data structure as long as Internally deployed Symbol.iterator attribute , Just It can be used for..of Loop through his members , in other words ,for..of The data structure is called inside the loop Symbol.iterator Method .
for..of The range that a loop can use includes arrays ,Set and Map structure .
Some array like objects ( Such as arguments object ,DOM NodeList object ),Genderator object , And strings .
and es5 Traversing objects for .. in similar
const arr = [" The tiger "," The tiger "," raw "," Wei "]
for(let item of arr){ console.log(item) }

const str = "2022"
for(let item of str){ console.log(item) }

const obj = { a:1 ,b:2, c:3 }
for(let item of obj){ console.log(item) } Report errors reason : Objects cannot be iterated

for .. of Realization principle
const obj = { a:1 ,b:2, c:3 }
obj[Symbol.iterator] = function(){
let i = 0;
const keys = Object.keys(this)
return {
next:()=>{
return i <= keys.length = 1 ? { value:this[keys[i++]] } : {value:undefined, done:true } } } }
for(let v og obj ){console.log(v)}

const obj = { a:1 ,b:2, c:3 }
obj[Symbol.iterator] = function(){
let i = 0;
const keys = Object.keys(this)
return {
next:()=>{
return i <= keys.length = 1 ? { value:keys[i++] } : {value:undefined, done:true } } } }
for(let v og obj ){console.log(v)}

边栏推荐
- [1184. Distance between bus stops]
- 【云原生之kubernetes】kubernetes集群高级资源对象statefulesets
- JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
- Luogu p2024 [noi2001] food chain
- Which securities account is the best and safest for beginners
- Upgrade the jdbc driver to version 8.0.28 and connect to the pit record of MySQL
- Talk about how redis handles requests
- Convert a string to an integer and don't double it
- 必会面试题:1.浅拷贝和深拷贝_浅拷贝
- ODBC executes stored procedure to get return value
猜你喜欢

Icassp 2022 | KS transformer for multimodal emotion recognition

VGA display based on FPGA

聊聊 Redis 是如何进行请求处理

把字符串转换成整数与不要二

The rule created by outlook mail is invalid. Possible reasons

凸优化基础知识

郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究

Notes of Teacher Li Hongyi's 2020 in-depth learning series 9

价值驱动为商业BP转型提供核心动力——业务场景下的BP实现-商业BP分享

Notes of Teacher Li Hongyi's 2020 in-depth learning series 2
随机推荐
Process / thread synchronization mechanism
价值驱动为商业BP转型提供核心动力——业务场景下的BP实现-商业BP分享
VC prompts to recompile every time you press F5 to run
Talk about how redis handles requests
百度网盘+Chrom插件
Error connecting MySQL database with kettle
Talk about how redis handles requests
Notes of Teacher Li Hongyi's 2020 in-depth learning series 5
芯片的功耗
Paper notes: accurate causal influence on discrete data
About the word 'don't remember'
Li Kou 1184. Distance between bus stops
【零基础】php代码审计之sql注入
Icassp 2022 | KS transformer for multimodal emotion recognition
Network Security Learning (III) basic DOS commands
ASP. Net core 6.0 data validation based on model validation
Use and partial explanation of QDIR class
How static code analysis works
Backgroundworker enables time-consuming operations without affecting interface response
郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究