Get the App
SLTechnology News&Howtos  ›  Development  › 

What does es6 generator mean?

Shulou Source: shulou.com Published: 2022-06-01 02:21:49 09月29日 Update

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.

Tags: Generation generator function iteration object meaning article ordinary between that is interfaces data more types expressions syntax different good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Huawei Shulou Tech Info OPPO Reno