ES6生成器:.next()的输入值

时间:2016-05-18 05:01:42

标签: javascript ecmascript-6

我跟随此awesome blog并且有一个简单的生成器示例,它为.next()方法发送了一个值:

var answers = [
  `It is certain`, `It is decidedly so`, `Without a doubt`,
  `Yes definitely`, `You may rely on it`, `As I see it, yes`,
  `Most likely`, `Outlook good`, `Yes`, `Signs point to yes`,
  `Reply hazy try again`, `Ask again later`, `Better not tell you now`,
  `Cannot predict now`, `Concentrate and ask again`,
  `Don't count on it`, `My reply is no`, `My sources say no`,
  `Outlook not so good`, `Very doubtful`
]
function answer () {
  return answers[Math.floor(Math.random() * answers.length)]
}

// The most relevant part
function* chat () {
  while (true) {
    let question = yield '[Genie] ' + answer()
    console.log(question)
  }
}

我真的无法理解这个输入将如何产生这个输出:

var g = chat()
g.next()
console.log(g.next('[Me] Will ES6 die a painful death?').value)
// <- '[Me] Will ES6 die a painful death?'
// <- '[Genie] My sources say no'

第一个输出来自哪里 - 结果的[Me]部分?

2 个答案:

答案 0 :(得分:5)

yield之后的表达式将退出(返回)。传递给next()的值进入,yield表达式求值为此值。请注意,虽然在第一个.next()调用中传入的任何值都会被丢弃但不会被使用。

  1. g.next()被称为

    let question = yield '[Genie] ' + answer()    
                         ^-------------------^
          execution is paused here, this expression is evaluated
          and the result will go out(returned)
               {value:"[Genie] ...",done:false}
    
  2. 然后调用g.next('[Me] Will ES6 die a painful death?')

    g.next('[Me] Will ES6 die a painful death?').value
           ^----------------------------------^
     string goes in, execution continues, yield expression 
          will be evaluated as the passed in string
    
     let question = yield '[Genie] ' + answer(); //becomes -v
     let question = '[Me] Will ES6 die a painful death?';
    
  3. question等于[Me]字符串

  4. questionconsole.log(question)
  5. 记录到控制台
  6. 执行继续,直到遇到下一个产量

    let question = yield '[Genie] ' + answer()    
                         ^-------------------^
          execution is paused here, this expression is evaluated
          and the result will go out(returned)
               {value:"[Genie] ...",done:false}
    
  7. 现在g.next('[Me] Will ES6 die a painful death?')评估为

    {value:"[Genie] ...",done:false}
    

    使最后一行评估如下:

    console.log( g.next('[Me] Will ES6 die a painful death?').value );
    //becomes 
    console.log( ({value:"[Genie] ...",done:false}).value );
    //which becomes
    console.log("[Genie] ...");
    

答案 1 :(得分:1)

对我来说清除了很多混淆的是MDN documentation for yield

  

语法

[rv] = yield [expression];
     

RV

     

返回传递给生成器next()方法的可选值,以恢复执行。

这就是为什么第二次调用next将决定let question的价值的原因。这就是我们恢复执行的地方。

然后,生成器打印出问题,继续循环的下一轮,产生下一个Genie的答案并停止。如果再次调用next,则传递给它的参数将决定let question的下一个值。