一、生成器函数其实就是一个特殊的函数,实习异步编程
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');