Javascript ES6生成器:输入.next()的值
下面是一个简单的生成器示例,它为Javascript ES6生成器:输入.next()的值,javascript,ecmascript-6,Javascript,Ecmascript 6,下面是一个简单的生成器示例,它为.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
.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'
var g=chat()
g、 下一个()
log(g.next(“[Me]ES6会痛苦地死去吗?”).value)
//yield
之后的表达式将消失(返回)。传递给next()
的值进入,而yield
表达式的计算结果就是这个值。请注意,尽管在第一个.next()调用中传入的任何值都会被丢弃而不使用
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}
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?';
g.next(“[Me]ES6会痛苦地死去吗?”)
let question = yield '[Genie] ' + answer()
^-------------------^
execution is paused here, this expression is evaluated
and the result will go out(returned)
{value:"[Genie] ...",done:false}
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?';
问题
等于[Me]
字符串question
通过console.log(question)
let question = yield '[Genie] ' + answer()
^-------------------^
execution is paused here, this expression is evaluated
and the result will go out(returned)
{value:"[Genie] ...",done:false}
g.next(“[Me]ES6会痛苦地死去吗?”)
{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] ...");
对我来说,消除了很多困惑的是 语法 房车 返回传递给生成器的next()方法以恢复其执行的可选值 这就是为什么对
next
的第二次调用将决定let question
的值。那就是我们继续执行死刑的地方
生成器然后打印出问题,继续下一轮循环,生成下一个精灵的答案并停止。如果再次调用
next
,传递给它的参数将确定let question
的下一个值。那么,传递到next(…)
的内容是yield
表达式的结果,将成为问题
,在下次遇到yield
之前记录的,会导致next
调用返回。但是当我调用.next()方法时,这个问题不是被覆盖了吗?我不明白为什么两个输出都会被打印出来。我不知道你说的“覆盖”是什么意思。其中一个输出来自question
(在发电机功能中),另一个来自g.next(…).value
(在发电机外部)。