Home>Article>Web Front-end> Detailed explanation of Generator generator in Javascript
A generator is some code that runs inside a function
After returning a value, it pauses itself, and -
The calling program can ask to unpause and return another value
This "return" is not a traditional slave functionreturn
. So it was given a special name -yield
.
Generator syntax varies from language to language. Javascript's generator syntax is similar to PHP's, but it's different enough that if you expect them to do the same thing, you'll end up getting very confused.
In javascript, if you want to use a generator, you need to:
Define a special generator function
Call this function to create a generator object
Use the generator object in a loop, or directly call itsnext
method
We use the following simple program as a starting point and perform each of the following steps:
// File: sample-program.js function *createGenerator() { for(let i=0;i<20;i++) { yield i } } const generator = createGenerator() console.log(generator.next()) console.log(generator.next())
If you run this code, you will get the following output:
$ node sample-program.js { value: 0, done: false } { value: 1, done: false }
Here I go Explain how the program works.
First, there is a definition of the generator function in the code:
function* createGenerator() { for(let i=0;i<20;i++) { yield i } }
function
followed by*
Tells javascript that this is a generator function. The following writings are all valid definitions of generator functions.
function*createGenerator function* createGenerator function *createGenerator
*
andare not part of thefunction name. Instead, thefunction*
symbols define generators.
After defining the generator function, we name it a function with another name.
// 注意:当调用时,没有 *。 * 不是函数名称的一部分 // `function *` 是用于定义生成器函数的符号 const generator = createGenerator()
But remember:createGenerator
The functionhas noreturn value. This is because generator functions don't have traditional return values. In contrast, when you call a generator function directly, italwaysreturns an instantiatedGenerator
object.
This generator object has anext
method. Callingnext
will run the code inside the generator function.
function* createGenerator() { for(let i=0;i<20;i++) { yield i } }
This is important enough to call it again. Calling a generator functiondirectly will notrun any code within the generator function. Instead, create a generator object. It callsnext
on the generator object, thereby calling the code in the generator function.
The first timenext
is called on a generator object, the internal code will run until theyield
statement occurs. Once execution reachesyield
, javascript willpausethe execution of that code, andnext
will return (i.e. to you, oryield
) An object containing the values in theyield
rows.
When you callnext
a second time (or third, fourth or even more times), the code will unpause and continue running (as in the last call) where it was interrupted). The variable (such asi
in this example) will retain its value. When the code reaches anotheryield
statement, the function pauses again and returns an object containing the yield value.
This is why we have to call twicenext
console.log(generator.next()) console.log(generator.next())
will get the following output:
{ value: 0, done: false } { value: 1, done: false }
After the code in the generator function is executed, Any future calls tonext
will return an object with the valueundefinedanddone
set totrue
.
{ value: undefined, done: true }
Althoughnext
can be called manually on the generator object, we mainly want to use it in a loop. Take a look at this slightly modified program.
// File: sample-program.js @highlightsyntax@jscript function *createGenerator() { for(let i=0;i<5;i++) { yield i } } const generator = createGenerator() for(const value of generator) { console.log(value) }
When using a generator object in afor...of
loop,next
is called on the generator object each time through the loop, with the resulting value Populate the variable (value
above). Running this program will output the following:
$ node sample-program.js 0 1 2 3 4
In the next article, we will take a deeper look atfor ... of
loops and explore how to provide a built-in Method to loop through any object in javascript
English original address: https://alanstorm.com/javascript-generators/
Author: Alan Storm
Translation Address: https://segmentfault.com/a/1190000023924834
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of Detailed explanation of Generator generator in Javascript. For more information, please follow other related articles on the PHP Chinese website!