当前位置:网站首页>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)}

边栏推荐
- Nvida tensorrt deployment (I)
- 激光雷达障碍物检测与追踪实战——cuda版欧式聚类
- P3201 [HNOI2009] 梦幻布丁 启发式合并
- Flex layout
- VC prompts to recompile every time you press F5 to run
- On the problem that the on-board relay cannot be switched on due to insufficient power supply
- 基于TensorFlow和Keras的卷积神经网络实现猫狗数据集分类实验
- Financial products can reach 6%. I want to open an account to buy financial products
- "Yuan universe 2086" outsold "San ti" in one-day sales and won the champion of JD books' one-day science fiction list
- The rule created by outlook mail is invalid. Possible reasons
猜你喜欢

NVIDA-TensorRT部署(一)

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

ShardingSphere-数据库分库分表简介

Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic

Oracle中实现对指定数据分组且获取重复次数

CA certificate production practice

Xiezhendong: Exploration and practice of digital transformation and upgrading of public transport industry
![[1184. Distance between bus stops]](/img/dd/3437e6a14ac02dac01c78b372a5498.png)
[1184. Distance between bus stops]

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

How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
随机推荐
Financial products can reach 6%. I want to open an account to buy financial products
The size of STM32 stack
burp从溯源到反制思路
Convex optimization Basics
谢振东:公共交通行业数字化转型升级的探索与实践
CA证书制作实战
Go基础笔记_4_map
About constant modifier const
"Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
ASP. Net core 6.0 data validation based on model validation
[1184. Distance between bus stops]
WPF uses pathgeometry to draw the hour hand and minute hand
The tragic experience of installing scikitlearn on win764
On the open and closed principle
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
这个月 google的“AI具备意识”的事件的想法
Backgroundworker enables time-consuming operations without affecting interface response
Understanding complexity and simple sorting operation
【1184. 公交站间的距离】
Talk about how redis handles requests