当前位置:网站首页>Syntax of generator function (state machine)
Syntax of generator function (state machine)
2022-07-07 15:41:00 【When can Xiaobai advance to success】
1、 Basic concepts
Generator function ( Generator function ) yes ES6 An asynchronous programming solution provided by , The grammatical behavior is completely different from traditional functions .
- Grammatically : First of all, it can be understood as a State machine , Encapsulates multiple internal states .
- Returns the traverser object : perform Generator The function returns one Traverser object , The returned iterator object can traverse in turn Generator Every state inside a function .
- Formally :Generator Function is a normal function , There are two characteristics :①function There is a between the command and the function name asterisk ;② Function body internal use yield Statement defines different internal states (yield Meaning of output )
function main()
{
var hw = DayGenerator();// Returns the traverser object
for(let i=0;i<3;i++)
{
console.log(hw.next().value);
}
}
function* DayGenerator()
{
// Code block 1
yield 'sunday';
// Code block 2
yield 'monday';
// Code block 3
return 'saturday';
}
Running results :
- Calling method :Generator The call method of a function is the same as that of a normal function , Also add a pair of parentheses after the function name .
- Whether to execute immediately , Return value : call Generator After the function , This function Not implemented . And it's not the result of the function , It is a pointer object that executes the internal state . namely Traverser object .
Every time you call next Method , The internal pointer is executed from the function header or the last stop , Until I hit the next one yield sentence ( or return sentence ). In other words ,Generator The function is Execute in sections Of ,yield Statement is a flag to pause execution , and next Method can resume execution .
2、yield expression
because Generator The only traverser object returned by the function is to call next Method to traverse the next internal state , So it actually provides A function that can pause execution .yield Statements are pause flags .
Of the traverser object next The operation logic of the method is as follows :
- encounter yield Statement pauses the execution of subsequent operations , And will follow closely yield After the expression as the return object value Property value .
- Next call next Method, and then continue to execute , Until I hit the next one yield sentence .
- If there is no new yield sentence , It runs until the end of the function , until return The statement so far , And will return The value of the expression after the statement is used as the value of the return object value Property value .
- If the function doesn't have return sentence , Returns the value The property value is undefined.
yield and return The difference between :
Similarities : Can return the value of the expression immediately after the statement .
The difference : encounter yield Function pause , Next time, it will continue to execute backward from this position ; and return The sentence does not have the function of location memory , A function can only be executed once return sentence , But it can be done many times yield sentence .
yeild Expression if used in another expression , Must be in parentheses
function* demo(){
console.log('Hello'+yield);//SyntaxError Grammar mistakes
console.log('Hello'+yield 123);//SyntaxError
console.log('Hello'+(yield));//ok
console.log('Hello'+(yield 123));//ok
}
yield An expression is used as a function parameter or placed to the right of an assignment expression , I don't have to put in parentheses .
function* demo(){
foo(yield 'a',yield 'b');//ok
let input = yield;
}
边栏推荐
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
- 如何在opensea批量发布NFT(Rinkeby测试网)
- Qu'est - ce qu'une violation de données
- 众昂矿业:萤石继续引领新能源市场增长
- [understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
- [deep learning] image hyperspectral experiment: srcnn/fsrcnn
- Matlab experience summary
- [Lanzhou University] information sharing of postgraduate entrance examination and re examination
- Webgl texture
- 15. Using the text editing tool VIM
猜你喜欢
【OBS】RTMPSockBuf_Fill, remote host closed connection.
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
一大波开源小抄来袭
What is Base64?
MongoD管理数据库的方法介绍
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
Mathematical modeling -- what is mathematical modeling
Unity之ASE实现卡通火焰
随机推荐
知否|两大风控最重要指标与客群好坏的关系分析
How to release NFT in batches in opensea (rinkeby test network)
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
Gd32 F3 pin mapping problem SW interface cannot be burned
Summer safety is very important! Emergency safety education enters kindergarten
webgl_ Enter the three-dimensional world (1)
MongoDB数据库基础知识整理
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Runnable是否可以中断
从 1.5 开始搭建一个微服务框架链路追踪 traceId
PAT 甲级 1103 Integer Factorizatio
Use cpolar to build a business website (2)
Starting from 1.5, build a microservice framework link tracking traceid
Monthly observation of internet medical field in May 2022
[target detection] yolov5 Runtong voc2007 data set
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
Window环境下配置Mongodb数据库
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
Do not use memset to clear floating-point numbers
Ctfshow, information collection: web13