In-depth understanding of js generator data types and jsgenerator

Source: Internet
Author: User

In-depth understanding of js generator data types and jsgenerator

1. Overview

Generator is a new data type introduced by ES6. It looks like a function. In addition to return, yield can return multiple times.

Generator is defined by function * (note ),

2. Example

The function cannot save the status, and sometimes the global variable is required to save the 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 <100; x ++) {if (g. next (). value! = X) {pass = false; alert ('test failed! '); Break ;}} if (pass) {alert (' test passed! ');}

2.2 an infinite loop iterator

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

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 }"g.next();   // "Object { value: undefined, done: true }"

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 }g.next();    // { value: undefined, done: true }

Note: If the value of done is true, call return. The returned value is undefined.

function* gen() {yield 1;}var g = gen();console.log(g.next());//{ value: 1, done: false }console.log(g.next());//{ value: undefined, done: true }console.log(g.return(1)); //{ value: undefined, done: true }

2.5 Generator. prototype. throw ()

The thorw () method re-obtains the execution of the iterator by throwing an exception into the iterator;

Returns an object with two attributes: value and done.

function* gen() { while(true) {  try {    yield 42;  } 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 in-depth understanding of the js generator data type is all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

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.