es6生成器

by Sanket Meghani

通过Sanket Meghani

ES6生成器 (ES6 Generators)

Generators are one of the key features introduced in ES6. Contrary to normal functions which can only be entered at the beginning of the function, Generators are functions which can be exited and re-entered later with their context (variable bindings) saved across re-entrances. In other words, Generator function could return a value midway and resume it’s execution from midway later on.

生成器是ES6中引入的关键功能之一。 与只能在函数开头输入的常规函数​​相反,生成器是可以退出并在以后重新输入的函数,它们的上下文(变量绑定)保存在重新输入之间。 换句话说,Generator函数可以在中途返回一个值,然后在中途恢复执行。

A generator can be defined using a function keyword followed by an asterisk.

可以使用function关键字后跟星号来定义生成器。

The difference between calling a normal function and an iterator function is that calling a generator function does not execute the generator function immediately. It returns an iterator object for the generator instead. To execute the generator body we need to call next() method on the returned iterator.

调用普通函数和迭代器函数的区别在于,调用生成器函数不会立即执行生成器函数。 它改为为生成器返回一个迭代器对象。 要执行生成器主体,我们需要在返回的迭代器上调用next()方法。

let generator = myFirstGenerator(5);let output = generator.next();

When the iterator’s next() method is called, the generator function's body is executed until the first yield expression. Yield expression specifies the value to be returned. In the example above, calling generator.next() would execute the first console.log() statement and return output of (a + 5). The next() method returns an object in following structure.

调用迭代器的next()方法时,将执行生成器函数的主体,直到第一个yield表达式为止。 Yield表达式指定要返回的值。 在上面的示例中,调用generator.next()将执行第一个console.log()语句并返回(a + 5)的输出。 next()方法返回以下结构的对象。

{    value: 10, //Return value of yield expression. I.e 5 + 5    done: false //Weather generator has yielded it's last value}

We can print the returned yielded value using value property of the returned object.

我们可以使用返回对象的value属性打印返回的产生的值。

let generator = myFirstGenerator(5);let output = generator.next(); //output = {value: 10, done: false}
console.log('Output is: ', output.value); //Output is: 10

Calling next() again on the iterator would continue execution of generator from last yield expression until next yield expression or a return statement is encountered.

在迭代器上再次调用next()将从上一个yield表达式继续执行生成器,直到遇到下一个yield表达式或return语句为止。

output = generator.next(10); //output = {value: 15, done: true}

In our example, calling generator.next(10) would resume generator execution from line 4 with last yield expression value being 10 (i.e value passed to next()). Hence line 4 would be evaluated as b = 5 + 10 resulting into b = 15. On line 7 it returns the value of b and since this is the last return statement (i.e: no more yields left), done is set to true.

在我们的示例中,调用generator.next(10)将从第4行恢复生成器执行,最后一个yield表达式值为10(即,传递给next()的值)。 因此,第4行将被评估为b = 5 + 10,从而得出b =15。在第7行,它将返回b的值,并且由于这是最后一个return语句(即:不再剩余收益),因此将done设置为true。

Calling the next() method with an argument will resume the generator function execution, replacing the yield statement where execution was paused with the argument from next().

用参数调用next()方法将恢复生成器函数的执行,并用next()的参数替换执行暂停的yield语句。

output = generator.next(15); //output = {value: 20, done: true}

Calling generator.next(15) would resume generator execution from line 4 with last yield expression value replaced by 15. Hence line 4 would be evaluated as b = 5 + 15 resulting into b = 20.

调用generator.next(15)将从第4行恢复生成器执行,最后一个yield表达式值替换为15。因此,第4行的计算结果为b = 5 + 15,结果为b = 20。

We can use yield* to delegate to another generator function.

我们可以使用yield *委托给另一个生成器函数。

面容 (Postface)

It is quite intriguing to know how this new feature could be used in practice. Redux-saga uses generator functions to make it easy to write asynchronous flows. We’ve just scratched the surface and there’s a lot more to them. I would love to hear your comments, suggestions or questions around ES6 generators and it’s use cases :).

知道如何在实践中使用此新功能非常有趣。 Redux-saga使用生成器函数使编写异步流变得容易。 我们只是从头开始,还有很多事情要做。 我很想听听您对ES6生成器及其用例的意见,建议或问题:)。

翻译自: https://www.freecodecamp.org/news/es6-generators-47a9c5290569/

es6生成器

es6生成器_ES6生成器相关推荐

  1. ES6 迭代器与生成器(非常详细、容易理解)

    下面是对ES6中迭代器和生成器的整理,非常详细.容易理解,希望可以帮助到有需要的小伙伴~ 文章目录 迭代器是什么 Iterator接口 迭代协议 for...of语句的用法 返回迭代器对象的方法 与f ...

  2. python生成器函数_【python】生成器和生成器函数

    1.生成器函数: 普通函数: def func(): print("周杰伦") return "昆凌" func() #执行func()函数,"周杰伦 ...

  3. 生成器和生成器表达式

    一 . 生成器 生成器就是迭代器 生成器的特点和迭代器一样. 1.省内存 2.惰性机制 3.只能向前 在python中有三种方式获取生成器 1.通过生成器函数 2.通过各种推导式来实现生成器 3.通过 ...

  4. 用python编写图片生成器_python生成器

    生成器 一.生成器 定义: 生成器与迭代器看成是一种.生成器的本质就是迭代器. 唯一的区别: 生成器是自己用python写代码构建的数据结构.迭代器都是(系统)提供的,或者转化而来的 获取生成器的三种 ...

  5. 自动论文生成器 python_python生成器

    python生成器 python高级特性 首先, 所有的生成器都是迭代器, 因为生成器完全实现了迭代器的接口. 在python中, 有两种不同的方式提供生成器: 1. 生成器表达式 2. 生成器函数 ...

  6. python生成器next_Python生成器生成next,python,yieldnext

    在实践keras网络模型的时候,发现keras fit_generator与flow_from_directory函数均有关于生成器的频繁使用:因此在本文中对其进行总结. Python 之所以要提供这 ...

  7. 初识生成器与生成器表达式 Day12

    一.生成器 1,生成器基本概念 生成器的实质是迭代器 迭代器:Iterator 内部同时包含了__iter__()和__next__()函数 可迭代对象:Iterable 内部包含__iter__() ...

  8. python生成器表达式_python 生成器和生成器表达式

    1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样.取值方式和迭代器一样(__next__(),send():给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创建 其实就是 ...

  9. Python中的yield关键字及表达式、生成器、生成器迭代器、生成器表达式详解

    文章目录 1. yield关键字及表达式.生成器.生成器迭代器.生成器表达式 1.1 yield关键字及表达式(yield expression) 1.1.1 yield关键字 1.1.2 yield ...

最新文章

  1. mysql怎么实现确认收货_Tp结合redis实现订单自动收货
  2. 合肥学院计算机原理,合肥学院计算机组成原理实验三-20210415130709.docx-原创力文档...
  3. Coins POJ - 1742(多重背包+是否装满问题)
  4. 翻译连载 | 附录 A:Transducing(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇...
  5. mysql大于等于怎么写_MySQL 对于千万级的大表要怎么优化?我写了6000字的深度解读...
  6. python串口模块找不到_有没有python的串口库
  7. Asp.Net(C#.VB)Array、ArrayList和List的区别
  8. Word页眉本来有但不可见 前后相连时
  9. 高翔《视觉SLAM十四讲》从理论到实践
  10. 服务器 启动多个nginx_Nginx工作原理和优化总结
  11. [Qt]一个关于galgame的练手项目的总结
  12. QListView的使用
  13. 教你一键如何更换证件照底色?
  14. 台式计算机关机后自行重启,台式电脑关机后自动重启该怎么解决
  15. Spring核心接口ObjectProvider
  16. 京东云服务器——免费体验6个月
  17. gradle project sync failed.please fix your project and try again-Android Studio3.1.2运行出错
  18. 【职场管理】如果公司要你轮岗,怎么办?
  19. win7下用VS2008写视频聊天程序,求VFW教程?qzvgK
  20. 使用CubeMX生成工程时Debug模式为No Debug与Serial Wire的代码差异

热门文章

  1. css 精灵图 0302
  2. mysql-演练0722
  3. jquery-ajax-jsonp-360搜索引擎的联想词获取
  4. sublime-安装插件
  5. 数据加密:RSA 密钥
  6. 理解*arg 、**kwargs
  7. 在ASP.NET中防止注入攻击[翻译]
  8. 《基于模型的软件开发》——1.2 结构化开发
  9. 《产品设计与开发(原书第5版)》——3.8 步骤5:选出最佳机会方案
  10. 一个热词推荐的简单实现