当前位置:网站首页>ES6 生成器
ES6 生成器
2022-08-05 05:13:00 【@前端攻城狮】
生成器
生成器对象
生成器对象由生成器函数返回,它符合可迭代协议和迭代器协议。
语法:
function* gen() {
yield 1;
yield 2;
yield 3;
}
let g = gen();
// "Generator { }"
Generator.prototype.next()
返回一个包含两个属性(done,value)的对象。
Generator.prototype.return(value)
返回一个对象,该对象的value值等于传入的值。
如果对已经处于“完成”状态的生成器调用return(value)
,则生成器将保持在“完成”状态。如果没有提供参数,则返回对象的value
属性与示例最后的next()
方法相同。如果提供了参数,则参数将被设置为返回对象的value
属性的值。
function* gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen();
g.next(); // { value: 1, done: false }
g.next(); // { value: 2, done: false }
g.next(); // { value: 3, done: false }
g.next(); // { value: undefined, done: true }
g.return(); // { value: undefined, done: true }
g.return(1); // { value: 1, done: true }
生成器函数
function* 定义一个生成器函数,返回一个生成器对象(Generator)。
调用一个生成器函数并不会马上执行它里面的语句,而是返回一个这个生成器的 迭代器对象。当这个迭代器的 next()
方法被首次(后续)调用时,其内的语句会执行到第一个(后续)出现yield
的位置为止,yield
后紧跟迭代器要返回的值。或者如果用的是 yield*
(多了个星号),则表示将执行权移交给另一个生成器函数(当前生成器暂停执行)。
给next()方法传参:
调用 next()
方法时,如果传入了参数,那么这个参数会传给上一条执行的 yield语句左边的变量:
function *gen(){
yield 10;
x=yield 'foo';
yield x;
}
var gen_obj=gen();
console.log(gen_obj.next());// 执行 yield 10,返回 10
console.log(gen_obj.next());// 执行 yield 'foo',返回 'foo'
console.log(gen_obj.next(100));// 将 100 赋给上一条 yield 'foo' 的左值,即执行 x=100,返回 100
console.log(gen_obj.next());// 执行完毕,value 为 undefined,done 为 true
显示返回:
当在生成器函数中显式 return
时,会导致生成器立即变为完成状态,即调用 next()
方法返回的对象的 done
为 true
。如果 return
后面跟了一个值,那么这个值会作为当前调用 next()
方法返回的 value 值。
function* yieldAndReturn() {
yield "Y";
return "R";//显式返回处,可以观察到 done 也立即变为了 true
yield "unreachable";// 不会被执行了
}
var gen = yieldAndReturn()
console.log(gen.next()); // { value: "Y", done: false }
console.log(gen.next()); // { value: "R", done: true }
console.log(gen.next()); // { value: undefined, done: true }
生成器函数可以接收参数:
function* idMaker(){
var index = arguments[0] || 0;
while(true)
yield index++;
}
var gen = idMaker(5);
console.log(gen.next().value); // 5
console.log(gen.next().value); // 6
yield*的使用:
function* anotherGenerator(i) {
yield i + 1;
yield i + 2;
yield i + 3;
}
function* generator(i){
yield i;
yield* anotherGenerator(i);// 移交执行权
yield i + 10;
}
var gen = generator(10);
console.log(gen.next().value); // 10
console.log(gen.next().value); // 11
console.log(gen.next().value); // 12
console.log(gen.next().value); // 13
console.log(gen.next().value); // 20
生成器函数不能当构造器使用:
function* f() {
}
var obj = new f(); // throws "TypeError: f is not a constructor"
使用生成器函数遍历多维数组并转换成一维数组:
function* iterArr(arr) {
//迭代器返回一个迭代器对象
if (Array.isArray(arr)) {
// 内节点
for(let i=0; i < arr.length; i++) {
yield* iterArr(arr[i]); // (*)递归
}
} else {
// 离开
yield arr;
}
}
// 使用 for-of 遍历:
var arr = ['a', ['b', 'c'], ['d', 'e']];
for(var x of iterArr(arr)) {
console.log(x); // a b c d e
}
// 或者直接将迭代器展开:
var arr = [ 'a', ['b',[ 'c', ['d', 'e']]]];
var gen = iterArr(arr);
arr = [...gen]; // ["a", "b", "c", "d", "e"]
边栏推荐
- uboot开启调试打印信息
- WPF中DataContext作用
- RL强化学习总结(一)
- LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
- vscode+pytorch使用经验记录(个人记录+不定时更新)
- Requests the library deployment and common function
- How does the Flutter TapGestureRecognizer work
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- The mall background management system based on Web design and implementation
- 【技能】长期更新
猜你喜欢
将照片形式的纸质公章转化为电子公章(不需要下载ps)
server disk array
Multi-threaded query results, add List collection
【过一下3】卷积&图像噪音&边缘&纹理
Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
jvm 三 之堆与栈
[cesium] 3D Tileset model is loaded and associated with the model tree
【过一下14】自习室的一天
Flutter学习2-dart学习
随机推荐
How does the Flutter TapGestureRecognizer work
Understanding and use of C# on set() and get() methods
【过一下7】全连接神经网络视频第一节的笔记
After controlling the export file in MySQL, it becomes \N. Is there any solution?
UVA10827
Wise Force Deleter强制删除工具
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
【过一下4】09-10_经典网络解析
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
结构光三维重建(一)条纹结构光三维重建
The underlying mechanism of the class
1.3 mysql batch insert data
【cesium】加载并定位 3D Tileset
【Untitled】
Flutter真机运行及模拟器运行
HQL语句执行过程
LAB 信号量实现细节
entry point injection
Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction