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 is the Generator function execution mechanism of ES6?

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

Share

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

This article mainly explains "what is the Generator function execution mechanism of ES6". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the Generator function execution mechanism of ES6?"

ES6 introduces a new Generator function, which can suspend the execution flow of the function through the yield keyword, which makes it possible to change the execution flow, thus providing a solution for asynchronous programming.

Generator function composition

Generator has two parts that distinguish it from ordinary functions.

First, after the function, there is a * before the function name.

There is an yield expression inside the function.

Where * is used to indicate that the function is a Generator function and yield is used to define the internal state of the function.

Function* func () {console.log ("one"); yield '1clients; console.log ("two"); yield' 2clients; console.log ("three"); return '3clients;} execution mechanism

Calling the Generator function is the same as calling an ordinary function by adding () to the function name, but instead of executing immediately like an ordinary function, the Generator function returns a pointer to the internal state object, so to call the next method of the ergodic object Iterator, the pointer starts from the function header or where it last stopped.

F.next (); / / one// {value: "1", done: false} f.next (); / / two// {value: "2", done: false} f.next (); / / three// {value: "3", done: true} f.next (); / {value: undefined, done: true}

When the next method is called for the first time, the execution starts from the head of the Generator function. First, the one is printed, then the execution stops when the yield is executed, and the value of the expression after yield is taken as the value of the value attribute of the returned object. At this time, the function has not finished execution, and the value of the done attribute of the returned object is false.

The second time you call the next method, the same as the previous step.

When the next method is called for the third time, the three is printed first, and then the return operation of the function is performed, and the value of the expression after return is taken as the value of the value attribute of the returned object. At this time, the function has ended, and the value of the done attribute is usually true.

When the next method is called for the fourth time, the function is finished, so the return value of the value property is undefined and the value of the done property is true. If there is no return statement when the third step is executed, {value: undefined, done: true} is returned directly.

The method next method of the ergodic object returned by the

In general, when the next method does not pass in parameters, the return value of the yield expression is undefined. When next passes in a parameter, it will be used as the return value of the previous step yield.

Next does not pass parameters

In addition to using next, you can also use for... The of loop iterates through the Iterator object produced by the Generator function.

Return method

The return method returns the given value and ends traversing the Generator function.

The return method returns a parameter when it is supplied, and undefined when no parameter is provided.

The ergodic object throws two errors, the first being caught inside the Generator function, and the second because the catch function inside the function body has been executed and will no longer catch the error, so the error is thrown out of the Generator function body and caught by the catch outside the function body.

Yield* expression

The yield* expression indicates that yield returns a traversal object that is used to call another Generator function inside the Generator function.

Working with scen

Implement Iterator

Provides traversal methods for objects that do not have an Iterator interface.

Reflect.ownKeys () returns all the properties of the object, regardless of whether the properties are enumerable or not, including Symbol.

Jane natively does not have Iterator interface and cannot pass for.... Of traversal. The Generator function is used here plus the Iterator interface, so you can traverse the jane object.

At this point, I believe you have a deeper understanding of "what is the execution mechanism of ES6's Generator function?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report