Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the characteristics of javascript generator function

Shulou Source: shulou.com Published: 2022-06-03 17:30:57 09月27日 Update

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!

Tags: Generation function generator feature learning statement location method more help iteration utility next parameters variables examples articles new biography theory knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Redmi MariaDB Docker