在ES6中,第一次调用迭代器的`next`方法时参数会发生什么? [英] In ES6, what happens to the arguments in the first call to an iterator's `next` method?

查看:181
本文介绍了在ES6中,第一次调用迭代器的`next`方法时参数会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有像这样的生成器,

If you have an generator like,

function* f () {
  // Before stuff.
  let a = yield 1;
  let b = yield 2;
  return [a,b];
}

并且,然后运行

var g = f();
// this question is over this value.
g.next(123); // returns: { value: 1, done: false }
g.next(456); // returns: { value: 2, done: false }
g.next(); // returns: { value: [ 456, undefined ], done: true }

第一次调用 .next() a 设置为 123 并且第二次调用将 b 设置为 456 ,但最后一次调用 .next() 这是返回,

The first call to .next() to set a to 123 and the second call to set b to 456, however at the last call to .next() this is return,

{ value: [ 456, undefined ], done: true }

第一次调用 g.next 迷路了?他们会怎么样?使用上面的例子,如何设置 a

Does the argument in the first call to g.next get lost? What happens to them? Using the above example, how do I set a?

推荐答案

尝试:

var g = f();
// this question is over this value.
g.next(); // returns: { value: 1, done: false }
g.next(123); // returns: { value: 2, done: false }
g.next(456); // returns: { value: [123, 456], done: true }

这篇关于在ES6中,第一次调用迭代器的`next`方法时参数会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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