ES6生成器 - 第一个next()没有yield表达式的示例 [英] ES6 Generators- Example where there is no yield expression for the first next()

查看:190
本文介绍了ES6生成器 - 第一个next()没有yield表达式的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ES6生成器,为什么此博文的作者说:

For ES6 generators, why does the author of this blog post say:

来自: http://davidwalsh.name/es6-generators


第一个下一个(..)调用,我们不发送任何东西,为什么?因为没有收益表达式来接收我们传入。

"The first next(..) call, we don't send in anything. Why? Because there's no yield expression to receive what we pass in."

第一个 it.next()调用(yield(x + 1))

function *foo(x) {
    var y = 2 * (yield (x + 1));
    var z = yield (y / 3);
    return (x + y + z);
}

var it = foo( 5 );

// note: not sending anything into `next()` here
console.log( it.next() );       // { value:6, done:false }
console.log( it.next( 12 ) );   // { value:8, done:false }
console.log( it.next( 13 ) );   // { value:42, done:true }




你可以看到我们仍然可以使用初始foo(5)iterator-instantiation调用传递参数(在我们的示例中为x),就像普通函数一样。

You can see that we can still pass in parameters (x in our example) with the initial foo( 5 ) iterator-instantiation call, just like with normal functions.

第一个..)打电话,我们不发送任何东西。为什么?因为没有收益表达式来接收我们传递的内容。

The first next(..) call, we don't send in anything. Why? Because there's no yield expression to receive what we pass in.


推荐答案

第一个 it.next()对应于 yield(x + 1),其结果为预期的6。下一次调用 it.next(12)中的12将第一个收益的值设置为12,因此 y 被设置为双倍或24,迭代器得到值(y / 3),这是8.最后调用 .next(13)将第二个收益的值设置为13,设置为 z ,并接收 return ,这是5 + 24 + 13。

The first it.next() corresponds to the yield(x + 1), which results in 6 as expected. The 12 in the next call to it.next(12) sets the value of that first yield to 12, so y is set to double that, or 24 and the iterator results in the value (y / 3), which is 8. The final call to it.next(13) sets the value of the second yield to 13, which is set to z, and receives the value of the return, which is 5 + 24 + 13.

由于语法

z = yield(y / 3)

其中某些东西看起来就像将 y / 3 分配给 z 。不是这样的。 y / 3 是正在产生的值作为迭代器的值,而 z 正在分配给值由跟随 it.next()调用传递,完全不同!省略括号可能会有所帮助,并将其写为

which somehow looks like one is assigning the value of something to do with y / 3 to z. That's not the case. y / 3 is the value being yielded to serve as the value of the iterator, whereas z is being assigned to the value passed in by the following it.next() call, something entirely different! It may be slightly helpful to omit the parentheses and write this as

var y = 2 * yield x + 1;
var z = yield y / 3;

而不是一个函数调用。

对于你提到的错误,例如,它是发送值到新生成器。当你想到它是有道理的。值作为参数发送到 it.next()成为生成器中最近收益的值。在第一次调用 it.next()时,在生成器中没有最新的收益,所以没有什么可以承担的价值通过,因此错误。

As for the error you mention, in traceur for example it is "Sent value to newborn generator". It makes sense when you think about it. The value send as a parameter to it.next() becomes the value of the most recent yield in the generator. On the first call to it.next(), there is no most recent yield in the generator, so there's nothing to take on the value being passed, hence the error.

不要将传递参数混淆到生成器( x 在您的情况下)这只是提供一种配置或初始化生成器的方法,将参数传递给 it.next(),它们作为最近的在生成器中生成

Don't confuse passing parameters to the generator (x in your case), which merely provides a way to configure or initialize the generator, with passing parameters to it.next(), which serve as the value of the most recent yield in the generator.

考虑如何编写等效的手动生成器可能会很有帮助(简化为仅返回下一个值,而不是 {value,done} ,当发生器不工作时抛出):

It may be helpful to consider how you would write the equivalent hand-rolled generator (simplified to just return the next value instead of {value, done}, and throwing when the generator is out of gas):

function foo(x) {
    var y, z, step = 0;
    return function next(val) {
        switch (step++) {
            case 0:               return x + 1;      break;
            case 1: y = 2 * val;  return y / 3;      break;
            case 2: z = val;      return x + y + z;  break;
            default: throw "generator finished";
        }
    };
}

然后:

iterator = foo(5);
iterator();             // 6
iterator(12);           // 8
iterator(13);           // 42

这篇关于ES6生成器 - 第一个next()没有yield表达式的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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