ES6 Generator data types in JavaScript _javascript tips

Source: Internet
Author: User
Tags generator

1. Generator Introduction

Generator is a new data type introduced by ES6 that looks like a function that yield can be returned multiple times except by returning with return.

Generator is defined by the function*, (note the * number),

2. Example

function cannot save state, sometimes need global variable to save number;

2.1

' Use strict ';
function next_id () {
var id = 1;
while (id<100) {
yield id;
id++;
}
return ID;
}
Test:
var
x, pass
= True,
g = next_id ();
for (x = 1; x < x + +) {
if (G.next (). Value!== x) {Pass
= false;
Alert (' Test failed! ');
break;
}
}
if (pass) {
alert (' Test passed! ');

2.2 An infinite loop of iterators

function* Idmaker () {
var index = 0;
while (true)
yield index++;
var Gen = Idmaker (); "Generator {}"
Console.log (Gen.next (). value);//0
Console.log (gen.next (). value);//1

2.3generator.prototype.next ()

When the iteration ends, Generator.next (). Done ===true, before the end ===false

Function* Gen () { 
yield 1;
Yield 2;
Yield 3;
}
var g = gen (); "Generator {}"
g.next ();//"Object {value:1, done:false}"
G.next ();//"Object {value:2, Done:false } "
G.next ();//" Object {value:3, done:false} "

2.4 Generator.prototype.return ();

The return method returns the given parameter value and ends the iterator

Example

Function* Gen () { 
yield 1;
Yield 2;
Yield 3;
}
var g = gen ();
G.next (); {value:1, done:false}
g.return ("foo");//{value: "foo", Done:true}

Note If the done value is true and then call return, the value returned is also undefined

Function* Gen () {yield 1;}
var g = gen ();
Console.log (G.next ());//{value:1, Done:false}
Console.log (G.next ());//{value:undefined, Done:true}

2.5 Generator.prototype.throw ()

Thorw () method, by throwing an exception into the iterator, to regain the execution of the iterator;

Returns an object with value and two properties

Function* Gen () {while
(true) {
try {
yield;
} catch (e) {
console.log ("Error caught!");
}
}
var g = gen ();
var a = G.next ();
{value:42, done:false}
var b = g.throw (new Error ("Something Went Wrong"));
"Error caught!"
{value:42, done:false}
Console.log (a);
Console.log (b.value+ "::" +b.done);

The above is a small set to introduce the JavaScript ES6 generator data types, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.