当前位置:网站首页>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;
}
边栏推荐
- 【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
- 【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- [quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
- [understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
- Qu'est - ce qu'une violation de données
- 2022年5月互联网医疗领域月度观察
- What are PV and UV? pv、uv
- What are the safest securities trading apps
- [quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
猜你喜欢
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
微信小程序 01
Gd32 F3 pin mapping problem SW interface cannot be burned
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
Super signature principle (fully automated super signature) [Yun Xiaoduo]
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
居然从408改考自命题!211华北电力大学(北京)
随机推荐
STM32F103C8T6 PWM驱动舵机(SG90)
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
2022年5月互联网医疗领域月度观察
Using eating in cocos Creator
【兰州大学】考研初试复试资料分享
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Android -- jetpack: the difference between livedata setValue and postvalue
[original] all management without assessment is nonsense!
Matlab experience summary
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
Nacos conformance protocol cp/ap/jraft/distro protocol
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
Cocos creator collision and collision callback do not take effect
HW初级流量监控,到底该怎么做
The rebound problem of using Scrollview in cocos Creator
众昂矿业:萤石继续引领新能源市场增长
MongoD管理数据库的方法介绍
简述keepalived工作原理
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?