第一个next()调用的参数在哪里? [英] Where argument of first next() call goes?

查看:117
本文介绍了第一个next()调用的参数在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的生成函数

I have a simple generator function

function *generate(arg) {
  console.log(arg)
  for(let i = 0; i < 3;i++) {
    console.log(yield i);
  }
}

然后我初始化生成器并尝试打印值控制台:

Then I init the generator and trying to print values in console:

var gen = generate('arg'); //doesn't print
gen.next('a'); // prints 'arg'
gen.next('b'); // prints 'b'
gen.next('c'); // prints 'c'
// ... nothing surprising later

a 从第一个 next() call go?有没有办法在发电机功能中使用它?

Where did argument a from first next() call go? Is there a way to utilize it inside generator function?

这是一个 Babel REPL ,你可以看到这个结果。

Here is a Babel REPL where you can see that result.

推荐答案

p> next 方法定义如下:

The next method is defined as follows:


25.3.1.2 Generator.prototype.next(value)

next 方法执行以下步骤:


  1. g 成为这个值。

  2. 返回 GeneratorResume g

  1. Let g be the this value.
  2. Return GeneratorResume(g, value).


GeneratorResume抽象操作在步骤10使用

The GeneratorResume abstract operation uses value at step 10:


25.3.3.3 GeneratorResume(generator,value)

具有参数generator和
值的抽象操作GeneratorResume执行以下步骤:

The abstract operation GeneratorResume with arguments generator and value performs the following steps:


  1. genContext 成为生成器的值的[[GeneratorContext]] 内部插槽

  1. Let genContext be the value of generator’s [[GeneratorContext]] internal slot.


  1. 恢复暂停评估 genContext 使用 NormalCompletion ( value )的操作,
    暂停它。让结果为恢复
    计算返回的值。

  1. Resume the suspended evaluation of genContext using NormalCompletion(value) as the result of the operation that suspended it. Let result be the value returned by the resumed computation.


第一种可能性是使用 yield (即suspendedYield state)来暂停评估。 。

The first possibility is that the evaluations was suspended by the use of yield (i.e. "suspendedYield" state).

其行为在 14.4.14运行时语义:评估


YieldExpression yield


  1. 返回 GeneratorYield CreateIterResultObject undefined
    false )) / li>
  1. Return GeneratorYield(CreateIterResultObject(undefined, false)).


(类似于 YieldExpressi在 yield AssignmentExpression

GeneratorYield 抽象操作挂起生成器,如下所示:

The GeneratorYield abstract operation suspends the generator as follows:



  1. 设置genContext 的代码评估状态,以便当评估恢复时一个完成 恢复价值将执行
    以下步骤:

  1. Set the code evaluation state of genContext such that when evaluation is resumed with a Completion resumptionValue the following steps will be performed:


  1. 返回 resumingValue 。 >
  2. 注意:这返回到最初称为抽象操作的 YieldExpression 生产的评估。

  1. Return resumptionValue.
  2. NOTE: This returns to the evaluation of the YieldExpression production that originally called this abstract operation.



所以作为第二个的参数传递的值被用作第一个的返回值 yield 表达式。并且作为第三个下一个的参数传递的值被用作第二个 yield 表达式的返回值。等等。

So the value passed as the argument of the second next is used as the returned value of the first yield expression. And the value passed as the argument of the 3rd next is used as the returned value of the 2nd yield expression. And so on.

然而,还有可能发电机尚未启动(即suspendedStart state)。

However, there is also the possibility that the generator hadn't started yet (i.e. "suspendedStart" state).

这是由 GeneratorStart 抽象操作:



  1. 设置 genContext 的代码评估状态,以便当针对执行上下文将执行以下
    步骤:

  1. Set the code evaluation state of genContext such that when evaluation is resumed for that execution context the following steps will be performed:


但是这些以下步骤不使用恢复值。

But those "following steps" don't use the resumption value.

因此,作为第一个<$ c的参数传递的值$ c> next 被丢弃。

Therefore, the value passed as the argument of the first next is discarded.

这篇关于第一个next()调用的参数在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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