Generator 函数

2023-10-29

基本概念

执行 Generator 函数会返回一个遍历器对象,Generator 函数除了状态机,还是一个遍历器对象生成函数。返回的遍历器对象,可以依次遍历 Generator 函数内部的每一个状态。

Generator 函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield表达式,定义不同的内部状态(yield在英语里的意思就是“产出”)

function* helloWorldGenerator() {
  yield 'hello';
  yield 'world';
  return 'ending';
}

var hw = helloWorldGenerator();

hw.next()
// { value: 'hello', done: false }

hw.next()
// { value: 'world', done: false }

hw.next()
// { value: 'ending', done: true }value属性就是当前yield表达式的值hello,done属性的值false,表示遍历还没有结束

hw.next()
// { value: undefined, done: true }

定义了一个 Generator 函数helloWorldGenerator,它内部有两个yield表达式(hello和world),即该函数有三个状态:hello,world 和 return 语句(结束执行)。

调用 Generator 函数后,该函数并不执行,返回是一个指向内部状态的指针对象。

调用遍历器对象的next方法,使得指针移向下一个状态。每次调用next方法,内部指针就从函数头部或上一次停下来的地方开始执行,直到遇到下一个yield表达式(或return语句)为止。
yield表达式是暂停执行的标记,next方法可以恢复执行。

遍历器对象的next方法的运行逻辑如下。

(1)遇到yield表达式,就暂停执行后面的操作,并将紧跟在yield后面的那个表达式的值,作为返回的对象的value属性值。

(2)下一次调用next方法时,再继续往下执行,直到遇到下一个yield表达式。

(3)如果没有再遇到新的yield表达式,就一直运行到函数结束,直到return语句为止,并将return语句后面的表达式的值,作为返回的对象的value属性值。

(4)如果该函数没有return语句,则返回的对象的value属性值为undefined。

不用yield表达式,就变成一个单纯的暂缓执行函数

function* f() {
  console.log('执行了!')
}

var generator = f();

setTimeout(function () {
  generator.next()
}, 2000);

yield表达式只能用在 Generator 函数里面

与 Iterator 接口的关系

任意一个对象的Symbol.iterator方法,等于该对象的遍历器生成函数,调用该函数会返回该对象的一个遍历器对象。

把 Generator 赋值给对象的Symbol.iterator属性,从而使得该对象具有 Iterator 接口。

next 方法的参数

next方法可以带一个参数,该参数就会被当作上一个yield表达式的返回值。

要第一次调用next方法时,就能够输入值,可以在 Generator 函数外面再包一层

for…of 循环

for…of循环可以自动遍历 Generator 函数运行时生成的Iterator对象,且此时不再需要调用next方法
for…of循环,扩展运算符(…)、解构赋值和Array.from方法内部调用的,都是遍历器接口。它们都可以将 Generator 函数返回的 Iterator 对象,作为参数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Generator 函数 的相关文章

随机推荐

  • Qt学习: QCloseEvent关闭事件的使用及代码示例

    QCloseEvent事件是指 当你鼠标点击窗口右上角的关闭按钮时 所触发的函数 如果你没有重写virtual closeEvent QCloseEvent event 这个虚函数的话 系统是默认接受关闭事件的 所以就会关闭窗体 但有的时候
  • ImportError: libcudart.so.10.0:cannot open shared object file: No such file or direct【mmdetection错误】

    问题 在使用mmdetection做训练的时候 出现错误 gt gt gt from mmdet apis import init detector Traceback most recent call last return bootst
  • 【IDEA】idea设置默认maven配置, 避免每次设置maven

    环境 IDEA 2018 2021 场景 每次导入新项目是 经常需要重新设置maven 非常麻烦 方案 idea设置默认maven配置 避免每次设置maven 方法 Step 1 打开Settings File gt Other Setti
  • cartographer 参数理解

    参考文章 cartographer参数调整 xjEzekiel 博客园 cartographer探秘第一章之安装编译与参数配置 李太白lx的博客 CSDN博客 cartographer 涉及到的参数需要增加删除或者修改尽量在velodyne
  • mysql connector net 5.0_mysql 数据库和net 的版本动态库搭配问题

    Connector NET 1 0 includes support for MySQL Server 4 0 4 1 and 5 0 features and full compatibility with the ADO NET dri
  • python实现弹球小游戏

    跟着趣味开发python一起实现的弹球小游戏 游戏运行效果 实现流程 1 创建游戏画布 创建ball类 2 增加几个动作 让小球移动 让小球来回反弹 改变小球的起始方向 3 加上球拍 使球拍左右移动 循环移动 4 增加输赢因素 对小球位置进
  • 运动补偿 & 运动估计

    运动补偿是一种描述相邻帧 相邻在这里表示在编码关系上相邻 在播放顺序上两帧未必相邻 差别的方法 具体来说是描述前面一帧 相邻在这里表示在编码关系上的前面 在播放顺序上未必在当前帧前面 的每个小块怎样移动到当前帧中的某个位置去 这种方法经常被
  • Effective Modern C++ Item 20 对于类似std::shared_ptr但有可能悬空的指针,使用std::weak_ptr

    如果需要某种智能指针能够像std shared ptr一样方便 但又无需参与管理所指涉到的对象的共享所有权的话 就很好适合用std weak ptr 但这样的功能同样会带来一个问题 这种指针需要处理一个对std shared ptr而言不是
  • softmax分类器_Softmax 理解

    Softmax深入理解 译 AIUAI www aiuai cn Pytorch的交叉熵nn CrossEntropyLoss在训练阶段 里面是内置了softmax操作的 因此只需要喂入原始的数据结果即可 不需要在之前再添加softmax层
  • OpenWrt-SDK-编译生成ipk软件包

    版本 Barrier Breaker 类型 brcm2708 下载SDK http downloads openwrt org barrier breaker 14 07 brcm2708 generic OpenWrt SDK brcm2
  • linux部署vue项目

    命令行进入配置文件 vi usr local nginx conf nginx conf 输入i进行修改端口号和文件路径 按ESC保存后输入 wq退出 进入sbin启动nginx cd usr local nginx sbin nginx
  • FMC164-基于JESD204B的4路1Gsps AD 4路1.25Gsps DA FMC子卡

    板卡介绍 FMC164子卡集成4通道1Gsps采样率 16位AD 4通道1 25Gsps 16位DA 板载时钟芯片HMC7044 可以提供JESD204B所需要的各种时钟 具有同步 触发功能 模拟信号采用SSMC射频连接器输入和输出 板卡可
  • es 修改mappings字段结构

    es不支持直接修改mappings字段结构 可以通过 reindex 重建索引 方式修改 POST reindex source index old index dest index new index op type create Ela
  • 记录:Qt Creator 10配置安卓开发环境

    Qt Creator 现在的安卓开发环境配置相比老版本方便了不少 本文以目前在线安装版的 Qt Creator 10 0 2 Qt 5 15 Qt 6 5 为例做演示 有些文件可能会因为网络问题需要科学上网才能下载 1 下载 JDK htt
  • 【css】css动画实现的3种方式

    css实现动画主要有3种方式 transition实现过渡动画 transform转变动画 animation实现自定义动画 一 transition过渡动画 1 语法 transition property duration timing
  • UnityAPI.Transform变换(Yanlz+Unity+API+Transform+)

    UnityAPI Transform变换 版本 作者 参与者 完成日期 备注 UnityAPI Transform V01 1 0 严立钻 2018 08 21 UnityAPI Transform变换 发布说明 UnityAPI Tran
  • Linux·C++多线程基础知识

    目录 1 多线程 1 1 多进程与多线程 1 2 多线程理解 1 3 创建线程 1 4 join与detach方式 1 join举例 2 detach举例 1 5 this thread 2 mutex 2 1 lock与unlock 2
  • 【Tensorflow】tf.nn.depthwise_conv2d如何实现深度卷积?

    实验环境 tensorflow版本1 2 0 python2 7 介绍 depthwise conv2d来源于深度可分离卷积 Xception Deep Learning with Depthwise Separable Convoluti
  • C#时间字符串转换

    class Program static void Main string args DateTime datetime DateTime Now 打印当前时间 Console WriteLine 时间为 n datetime n 方法1
  • Generator 函数

    Generator 函数 基本概念 与 Iterator 接口的关系 next 方法的参数 for of 循环 Generator prototype throw Generator prototype return next throw