ES6-Generator基础用法
Generator简介:
生成器,本身是函数,执行后返回迭代对象,函数内部要配合yield使用Generator函数会分段执行,遇到yield暂停。
使用Generator注意点:function 和函数名之间需要带 *
function* text(){
}
Generator的yield注意点:yield是ES6新关键字,作用是使Generator(生成器)函数暂停。
function* text(){
yield 'a';
yield 'b';
yield 'c';
return 'd';
}
let iterationObj = text();
console.log(iterationObj.next());//{value: "a", done: false}
console.log(iterationObj.next());//{value: "b", done: false}
console.log(iterationObj.next());//{value: "c", done: false}
console.log(iterationObj.next());//{value: "d", done: true}
yield后,必须return最后一个值,如果不return最后一个值value为undefined。
当每次执行后返回{value, done}value值是当次执行yield里面的值,done值是当次执行看看代码执行到第几行,如果到最后一行返回true,其他返回false
那么我们如何证明当遇到yield函数暂停。
function* text(){
yield 'a';
console.log('1');
yield 'b';
console.log('2');
yield 'c';
console.log('3');
return 'd'
}
let iterationObj = text();
console.log(iterationObj.next());//因为第一行是yield碰到暂停输出:{value: "a", done: false}
console.log(iterationObj.next());//执行第二行输出:1 然后碰到yield暂停输出:{value: "b", done: false}
当yield被变量接收,下一次执行参数就是上一个变量的值。
function* text(){
let value = yield 'a';
console.log(value);
let value1 = yield 'b';
console.log(value1);
let value2 = yield 'c';
console.log(value2);
return 'd'
}
let iterationObj = text();
console.log(iterationObj.next());//{value: "a", done: false} 因为第一个是执行yield所以直接暂停不输出第二行的console.log
console.log(iterationObj.next('22'));
那么打印结果就是{value: "a", done: false}
22
{value: "b", done: false}
ES6-Generator基础用法的更多相关文章
- ES6 generator 基础
参考文档 harmony:generators Generator是ES6的新特性,通过yield关键字,可以让函数的执行流挂起,那么便为改变执行流程提供了可能. 创建Generator functi ...
- es6 generator 基础知识
1.定义和使用 function *gen() { return 'first generator'; } // 有点类似类的实例化过程 let generatorResult = gen() // ...
- asyncio 基础用法
asyncio 基础用法 python也是在python 3.4中引入了协程的概念.也通过这次整理更加深刻理解这个模块的使用 asyncio 是干什么的? asyncio是Python 3.4版本引入 ...
- PropertyGrid控件由浅入深(二):基础用法
目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...
- logstash安装与基础用法
若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...
- elasticsearch安装与基础用法
来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...
- BigDecimal最基础用法
BigDecimal最基础用法 用字符串生成的BigDecimal是不会丢精度的. 简单除法. public class DemoBigDecimal { public static void mai ...
- Vue组件基础用法
前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...
- Smarty基础用法
一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...
- 前端自动化测试神器-Katalon的基础用法
前言 最近由于在工作中需要通过Web端的功能进行一次大批量的操作,数据量大概在5000左右,如果手动处理, 完成一条数据的操作用时在20秒左右的话,大概需要4-5个人/天的工作量(假设一天8小时的工作 ...
随机推荐
- 使用eclipse git插件合并merge部分代码方法
当有一个父项目,它的下面有多个子项目:或者一个项目下边,只想合并部分路径,甚至部分文件的内容,使用下边的方法可以达到目的,特此记录: 1.主项目右键 -> team -> remove f ...
- centos7.x下环境搭建(二)—nginx安装
上篇文章是对mysql的安装,接着上篇文章,这篇文章安装nginx服务 添加yum源 默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址.因此可以如下执行命令添 ...
- NetCore HttpClient The SSL connection could not be established, see inner exception
之前遇到一个问题 https://www.cnblogs.com/leoxjy/p/10201046.html 在centos 7.x HttpClient访问会出问题 The SSL conne ...
- RESTful规范总结
思维导图xmind文件:https://files-cdn.cnblogs.com/files/benjieming/RESTful%E8%A7%84%E8%8C%83.zip
- PHP错误日志相关
https://cloud.tencent.com/developer/article/1167951 php错误日志总结 https://cloud.tencent.com/developer/ ...
- Vue配置路由和传参方式及路由守卫!
安装路由 npm i vue-router -S 引入路由 import VueRouter form VueRouter 注入路由模块 Vue.use(VueRouter) 定义路由匹配规则 let ...
- Object.entries和Object.fromEntries
语法 Object.entries(obj) 参数 obj 可以返回其可枚举属性的键值对的对象. 返回值 给定对象自身可枚举属性的键值对数组 语法 Object.fromEntries(iterabl ...
- 达梦DCI
/************************************************************************/ /* DCI编程实例 */ /********** ...
- Shell 编程 函数
本篇主要写一些shell脚本函数的使用. 函数调用 #!/bin/bash sum(){ s=`expr 2 + 3` echo $s } sum [root@localhost ~]# vim su ...
- 【SSH错误】ssh_exchange_identification: read: Connection reset by peer
进行远程登录时,ssh root@xxxxxxxxx出现如下错误 ssh_exchange_identification: read: Connection reset by peer 解决方案:登录 ...