ES6生成器:首次调用next() [英] ES6 Generators: First call to next()

查看:85
本文介绍了ES6生成器:首次调用next()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何使用ES6 Generator函数.除了一个关于在传递参数时进行一个空的next()函数调用的概念之外,这似乎很简单.这是我从Mozilla文档中引用的代码.

I was trying to understand how to use ES6 Generator functions. It seems pretty straight forward except for this one concept about making an empty next() function call while passing arguments. Here is the code I'm referring to from Mozilla docs.

function* logGenerator() {
  console.log(yield);
  console.log(yield);
  console.log(yield);
}

var gen = logGenerator();

// the first call of next executes from the start of the function
// until the first yield statement

gen.next();
gen.next('pretzel'); // pretzel
gen.next('california'); // california
gen.next('mayonnaise'); // mayonnaise

据我了解,该代码仅执行到第一个yield语句,因此什么也不返回,然后第二次调用next(),该代码将执行到第二个yield,其中包括第一个yield线,因此pretzel已记录到控制台.

From what I understand, the code is executed only until the 1st yield statement so nothing is returned and then for the 2nd time we call next(), the code is executed until the 2nd yield which includes the 1st yield line, hence pretzel is logged to console.

如果是这种情况,那么在下面提到的代码中,0如何记录在对next()的第一次调用中?我在这里想念什么:(

If this is the case, in the code mentioned below how is 0 getting logged in the 1st call to next() ? I'm missing something here :(

function* idMaker() {
  var index = 0;
  while (index < 3)
    yield index++;
}

var gen = idMaker();

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined

参考文献: Mozilla文档

推荐答案

生成器函数有点特殊.它们可以在执行过程中多次接受和返回值,而常规"函数只能接受一组固定的参数,并返回单个值.在您的第一个示例中,它们用于将数据传递给生成器(多次传递参数),而在第二个示例中,它们用于相反的方式(多次返回值).

Well, generator functions are kind of special. They can accept and return values multiple times throughout its execution, while 'normal' functions can only accept a fixed set of parameters, and return a single value. In your first example they are used to pass data to the generator (passing parameters multiple times), while in the second example its the other way around (returning values a several times).

请考虑以下示例:

function* foo() {
    console.log("before a");

    var a = (yield 123);

    console.log("after a");

    yield (a * 2);

    console.log("end of function");
}

var bar = foo();

var x = bar.next(); // Runs until the first yield, prints 'before a'.
console.log(x.value); // 123
var y = bar.next(4); // Runs until the second yield, so prints 'after a'. 
console.log(y.value); // 8
var z = bar.next(); // prints 'end of function'.
console.log(z.done); // true

我们在第一个next()调用中不传递任何数据,直到第一个yield语句运行.由于yield 123,调用(x.value)的结果为123.下次我们使用4作为参数值调用next()时,将在生成器函数内填充局部变量a.执行代码,直到下一个yield语句并返回8的结果.

We pass in no data on the first next() call, letting it run until the first yield statement. Because of yield 123 the result of the call (x.value) is 123. The next time we call next() using 4 as parameter value we fill in local variable a inside the generator function. Code is executed until the next yield statement and returns the result of 8.

有关为何使用0而不是1的说明,请参见其他答案.

For the explanation why 0 instead of 1, see the other answer.

这篇关于ES6生成器:首次调用next()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆