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 does es6 generator mean?

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

Share

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

This article will explain in detail what es6 generator means, Xiaobian thinks it is quite practical, so share it with you for reference, I hope you can gain something after reading this article.

In es6, a generator is a function that sets an iterator, and calling the generator function returns an iterable object; there is an "*" sign between the function and the function name in the generator, and the yield expression is used inside the function body, with the syntax "function*gen(){yield1;yield2;}."

Operating environment for this tutorial: Windows 10, ECMAScript version 6.0, Dell G3 PC.

What does ES6 generator mean?

Generator is a function used to set iterators in Es6, so we can understand that the generator finally generates iterators.

Generator is a new data type introduced by ES6 standard.

A generator looks like a function, but can return multiple times.

There are two differences between the generator function and the ordinary function.

1: There is an * between function and function name,

2: The yield expression is used inside the function body

Calling a generator function is different from calling a normal function. Calling a generator function returns a generator object (iterable object).

Generator: There is an iterator interface in the generator, that is, the generator itself can iterate, that is, creating a generator is equivalent to creating an iterator. As for the purpose of generators, for some data types without iterator interfaces, we can use generators to generate an iterator custom to iterate.

That's my understanding of the generator itself. Here's the syntax.

Some properties of the generator

//generator declaration function* generator(){ }let test = generator(); console.log(test);//yield keyword in generator function* Generator(){ yield 100; yield 200; yield 300; yield 400; } let test = Generator(); console.log(test.next()); console.log(test.next()); console.log(test.next()); console.log(test.next()); console.log(test.next()); console.log(test.next()); console.log(test.next());

End result:

Parsing: Since there is next() in the prototype of the generator instantiation object, when next() is executed, it iterates according to yield.

About "es6 generator refers to what means" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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