Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of javascript generator function

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the characteristics of the javascript generator function". In the daily operation, I believe many people have doubts about the characteristics of the javascript generator function. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful for you to answer the doubt about "what are the characteristics of the javascript generator function?" Next, please follow the editor to study!

1. Generate a function call that does not execute the function body in the function body, but returns a generator.

Because the execution of the internal function of the generator function is controlled by the returned generator.

2. The generator function runs from the location of the last yield statement to the location of the next yield statement.

Whenever the next method of the returned generator is called.

Example

Function* createGenerator () {console.log ('function start...') Let res = yield 1 / / the first iteration is stuck in the yield statement The newly passed parameter values in the second iteration are assigned to the res variable (undefined if not passed) console.log ('logger-1, res) res = yield 2 console.log (' logger-2, res) res = yield 3 console.log ('logger-3') Res) return {desc: 'function end...'}} const generator = createGenerator () / / get generator generator.next / * print:' function start...'returns: {value: 1, done: false} * / generator.next / * print: 'logger-1' 222returns: {value: 2, done: false} * / generator.next () / * print: 'logger-2' undefinedreturns: {value: 3 Done: false} * / generator.next / * print: 'logger-3' 444returns: {value: {desc: 'function end...'}, done: true} * / so far On the "what are the characteristics of the javascript generator function" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report