es6generator
yield语句
由于Generator函数返回的遍历器对象,只有调用next
方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数。yield
语句就是暂停标志。
yield
语句只能用在 Generator 函数里面,用在其他地方都会报错。
yield
语句如果用在一个表达式之中,必须放在圆括号里面。yield
语句用作函数参数或放在赋值表达式的右边,可以不加括号。
function* demo() {
console.log('Hello' + yield); // SyntaxError
console.log('Hello' + yield 123); // SyntaxError console.log('Hello' + (yield)); // OK
console.log('Hello' + (yield 123)); // OK
}
function* demo() {
foo(yield 'a', yield 'b'); // OK
let input = yield; // OK
}
next方法的参数
yield
句本身没有返回值,或者说总是返回undefined
。next
方法可以带一个参数,该参数就会被当作上一个yield
语句的返回值。
function* foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
} var a = foo(5);
a.next() // Object{value:6, done:false}
a.next() // Object{value:NaN, done:false}
a.next() // Object{value:NaN, done:true} var b = foo(5);
b.next() // { value:6, done:false }
b.next(12) // { value:8, done:false }
b.next(13) // { value:42, done:true }
for...of循环
for...of
循环可以自动遍历Generator函数时生成的Iterator
对象,且此时不再需要调用next
方法。
一旦next
方法的返回对象的done
属性为true
,for...of
循环就会中止,且不包含该返回对象。
除了for...of
循环以外,扩展运算符(...
)、解构赋值和Array.from
方法内部调用的,都是遍历器接口。
function* numbers () {
yield 1
yield 2
return 3
yield 4
} // 扩展运算符
[...numbers()] // [1, 2] // Array.from 方法
Array.from(numbers()) // [1, 2] // 解构赋值
let [x, y] = numbers();
x // 1
y // 2 // for...of 循环
for (let n of numbers()) {
console.log(n)
}
// 1
// 2
Generator.prototype.return()
Generator函数返回的遍历器对象,还有一个return
方法,可以返回给定的值,并且终结遍历Generator函数。
function* gen() {
yield 1;
yield 2;
yield 3;
} var g = gen(); g.next() // { value: 1, done: false }
g.return('foo') // { value: "foo", done: true }
g.next() // { value: undefined, done: true }
如果Generator函数内部有try...finally
代码块,那么return
方法会推迟到finally
代码块执行完再执行。
这意味着,它们都可以将Generator函数返回的Iterator对象,作为参数。
function* numbers () {
yield 1;
try {
yield 2;
yield 3;
} finally {
yield 4;
yield 5;
}
yield 6;
}
var g = numbers()
g.next() // { done: false, value: 1 }
g.next() // { done: false, value: 2 }
g.return(7) // { done: false, value: 4 }
g.next() // { done: false, value: 5 }
g.next() // { done: true, value: 7 }
yield* 语句
在 Generator 函数内部,调用另一个 Generator 函数。
function* bar() {
yield 'x';
yield* foo();
yield 'y';
} // 等同于
function* bar() {
yield 'x';
yield 'a';
yield 'b';
yield 'y';
} // 等同于
function* bar() {
yield 'x';
for (let v of foo()) {
yield v;
}
yield 'y';
} for (let v of bar()){
console.log(v);
}
// "x"
// "a"
// "b"
// "y"
任何数据结构只要有Iterator接口,就可以被yield*
遍历。
应用
(1)异步操作的同步化表达
(2)控制流管理
(3)部署Iterator接口
利用Generator函数,可以在任意对象上部署Iterator接口。
function* iterEntries(obj) {
let keys = Object.keys(obj);
for (let i=0; i < keys.length; i++) {
let key = keys[i];
yield [key, obj[key]];
}
} let myObj = { foo: 3, bar: 7 }; for (let [key, value] of iterEntries(myObj)) {
console.log(key, value);
} // foo 3
// bar 7
对数组部署Iterator接口
function* makeSimpleGenerator(array){
var nextIndex = 0; while(nextIndex < array.length){
yield array[nextIndex++];
}
} var gen = makeSimpleGenerator(['yo', 'ya']); gen.next().value // 'yo'
gen.next().value // 'ya'
gen.next().done // true
(4)作为数据结构
es6generator的更多相关文章
- ES6-Generator
Generator 关键词:状态机,遍历器,同步方式写异步方法 基本概念 形式上,Generator函数是一个普通函数,但是有两个特征. function关键字与函数名之间有一个星号. 二是,函数体内 ...
- es6- Generator函数实现长轮询
1.Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同. 语法上,首先可以把它理解成,Generator 函数是一个状态机,封装了多个内部状态.形式上,Gene ...
- ES6-Generator使用与改写
用Generator封装Symbol中的iterator方法: 注意:Generator的function后必须写* config:分别有3个txt文件,两个文件写路径,一个文件写要输出的内容 前置写 ...
- ES6-Generator基础用法
Generator简介: 生成器,本身是函数,执行后返回迭代对象,函数内部要配合yield使用Generator函数会分段执行,遇到yield暂停. 使用Generator注意点:function 和 ...
随机推荐
- shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化
编写shell脚本难免遇到需要交互式输入指令的步骤: 方法一: # cat action.sh #!/bin/sh read -p "enter number:" no; read ...
- git 新建工程
之前的一篇文章 https://www.cnblogs.com/wjw-blog/p/7189730.html,按照流程能搭建好git仓库,有时候会有一些小问题. 按照这个流程:-在github 上新 ...
- VS2015工具箱不出现ArcGIS Windows Forms怎么办?
原文: https://blog.csdn.net/pangpi814961437/article/details/7954033
- URL编码问题
一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号. 比如,世界上有英文字母的网址"http://www.abc.com", 但是没有希腊字母的网址 ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- Linux snprintf使用总结
snprintf()函数用于将格式化的数据写入字符串,其原型为: int snprintf(char *str, int n, char * format [, argument, ...]); ...
- 基于JAVA语言的selenium总结
目录一.基本语句 1.循环控制(break,continue) 3.字符的替换(replace,repalceFirst,replaceAll,regex) 4.字符串的连接("+" ...
- Jenkins send build artifacts over ssh配置
配置jenkins远程部署的时候遇到的配置问题: 首先在系统设置-系统设置-Publish over SSH-SSH Server中配置服务器信息 配置完成后可以点击Test Configuratio ...
- 别让Open Sans字体拖慢wordpress后台速度
最近打开wordpress后台是不是很慢?国内GG登不上了?这两者有没什么直接的联系?没错,WordPress后台是自动加载的谷歌Open Sans字体,据说gg服务器已经迁移到阿嘛丽可,需要一些小手 ...
- Python3学习之路~4.3 装饰器
定义:本质是函数,装饰其他函数就是为其他函数添加附加功能. 原则: 不能修改被装饰函数的源代码 不能修改被装饰函数的调用方式 实现装饰器知识储备: 函数即“变量” 高阶函数 把一个函数名当做实参传递给 ...