ES6 生成器函数

一、生成器函数其实就是一个特殊的函数,实习异步编程

javascript 复制代码
function * gen() {
    console.log(111);
    yield '一只没有耳朵';
    console.log(222);
    yield '一只没有尾巴';
    console.log(3333);
    yield '真奇怪';
}

let iterator = gen();
iterator.next();  //执行的是函数开始 ------ yield '一只没有耳朵'中间的部分
iterator.next();  //执行的是yield '一只没有耳朵' ------  yield '一只没有尾巴'中间的部分

// 遍历
for (let v of gen()) {
    console.log(v);   // v是yield后面的部分
}

二、生成器函数的传参

javascript 复制代码
function * gen(arg) {
    console.log(arg);   // AAA
    let one = yield 111;
    console.log(one);   // BBB
    let two = yield 222;
    console.log(two);   // CCC
}

let iterator = gen('AAA');
iterator.next('BBB');
iterator.next('CCC');
相关推荐
jiayong234 天前
前端面试题库 - ES6+新特性篇
前端·面试·es6
研☆香10 天前
es6的新特性介绍
前端·ecmascript·es6
前端若水16 天前
安装 markdown-it 后项目报错,可能是 Vue/Webpack 项目中 Quill 的问题(ES6+ 语法不支持)
vue.js·webpack·es6
吴声子夜歌24 天前
Vue3——路由管理
前端·vue·es6·vue-router
普通网友25 天前
ES6模块化、Promise、async、await、EventLoop、API接口案例_export function 与 await
前端·ecmascript·es6
身如柳絮随风扬1 个月前
前端基础进阶:Node.js + ES6 + Axios + Vue 全面入门指南
前端·node.js·es6
吴声子夜歌1 个月前
Vue3——Vuex状态管理
前端·vue.js·vue·es6
军军君011 个月前
数字孪生监控大屏实战模板:云数据中心展示平台
前端·javascript·vue.js·typescript·前端框架·es6·echarts
研☆香1 个月前
es6的一些更新特点介绍
前端·ecmascript·es6
布局呆星1 个月前
Vue3 笔记:过渡动画与自定义指令
javascript·css·vue.js·python·es6·html5