当前位置:网站首页>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"]
边栏推荐
猜你喜欢
随机推荐
WPF中DataContext作用
开发一套高容错分布式系统
Flutter learning 5-integration-packaging-publish
redis 缓存清除策略
Flutter TapGestureRecognizer 如何工作
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
第三讲 Gradient Tutorial梯度下降与随机梯度下降
Error creating bean with name 'configDataContextRefresher' defined in class path resource
How to quickly upgrade your Taobao account to a higher level
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
Excel画图
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
RDD和DataFrame和Dataset
Flutter学习-开篇
Judgment statement _switch and case
span标签和p标签的区别
2022 The 4th C.Easy Counting Problem (EGF+NTT)
使用二维码解决固定资产管理的难题
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!









