当前位置:网站首页>Iterator, generator generator details
Iterator, generator generator details
2022-06-12 12:33:00 【Fashion_ Barry】
Talking about Generator( generator ) Before , Let's first understand another concept , iterator
One 、 iterator Iterator
iterator Iterator yes ES6 A new traversal mechanism is introduced , It is also a special object , It has some proprietary interfaces designed specifically for iterative processes ;
Each iterator object There is one. next() Method , Each call will return a current result object .
There are two properties in the current result object :
1.value: The value of the current property
2.done: Used to determine whether traversal ends , When there is no more data to return , return true
Every call next() Method , Will return the next available value , Know that the traversal is over .
Two 、 generator Generator
A generator is a function that returns an iterator , adopt function After the keyword ( * ) To express , New keywords will be used in functions yield . The asterisk can be next to function keyword , You can also add a space in the middle .
function* generator(){
const list = [1,2,3];
for(let i of list){
yield i
}
}
let g = generator();
console.log(g.next()); // {value: 1, done: false}
console.log(g.next()); // {value: 1, done: false}
console.log(g.next()); // {value: 1, done: false}
console.log(g.next()); // {value: 1, done: true}3、 ... and 、 characteristic
1. Every time I finish one yield The function will automatically stop running after the statement , Until it's called again next();
2. yield Keywords can only be used in generators (Generator) For internal use , Using it elsewhere causes the program to throw an error ;
3. You can use function expressions to create generators , But you can't use the arrow function
The most common place is in stream When processing data streams , Iterators are used !

边栏推荐
- Boot entry directory
- Advanced C language -- storage of deep anatomical data in memory (with exercise)
- 一个ES设置操作引发的“血案”
- Records of gdcpc Guangdong Provincial Games in 22 years
- A short guide to SSH port forwarding
- LDAP和SSO集成能实现什么效果?
- ITK Examples/RegistrationITKv4/DeformableRegistration
- C语言进阶篇——浮点型在内存中的存储
- You can't just use console Log ()?
- AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
猜你喜欢

NewOJ Week 10题解

Advanced C language -- storage of floating point in memory

Numpy numerical calculation basis

【您编码,我修复】WhiteSource正式更名为Mend

深度剖析指针的进阶——C语言的进阶篇

JS string array converted to numeric array and how to add the numbers in the array

Promise understanding has used promise to realize picture preloading (sequential loading)

JS attribute operation and node operation

Async/await for ES6

元宇宙是短炒,还是未来趋势?
随机推荐
拿来就能用的网页动画特效,不来看看?
Bank layout meta universe: digital collections, digital employees become the main track!
JS built in object
TRON-api-波场转账查询接口-PHP版本-基于ThinkPHP5封装-附带接口文档-20220528版本
Traditional DOM rendering?
屏蔽不显示VS警告warning
机械臂雅可比矩阵IK
NDT配准原理
sublime_ Textuse
A short guide to SSH port forwarding
golang的channel和条件变量在单生产单消费场景下的性能对比测试
In navigation, the solution of adding borders to affect the layout
AutoLock 解决加锁后忘记解锁问题
You can't just use console Log ()?
Promise understanding has used promise to realize picture preloading (sequential loading)
Rust语言学习
Imx6 uboot add lvds1 display
Redis的主从复制原理
NDT registration principle
Invalid date of moment conversion timestamp