首先能够把它理解成,Generator 函数是一个状态机,封装了多个内部状态。执行 Generator 函数会返回一个遍历器对象,也就是说,Generator 函数除了状态机,仍是一个遍历器对象生成函数。返回的遍历器对象,能够依次遍历 Generator 函数内部的每个状态。函数
Generator 函数是一个普通函数,可是有两个特征。this
一是,function关键字与函数名之间有一个星号; 二是,函数体内部使用yield表达式,定义不一样的内部状态(yield在英语里的意思就是“产出”)。
Generator 函数的调用方法与普通函数同样,也是在函数名后面加上一对圆括号。不一样的是,调用 Generator 函数后,该函数并不执行,返回的也不是函数运行结果,而是一个指向内部状态的指针对象,也就是上一章介绍的遍历器对象(Iterator Object)。咱们必须调用遍历器对象的next方法,使得指针移向下一个状态。也就是说,每次调用next方法,内部指针就从函数头部或上一次停下来的地方开始执行,直到遇到下一个yield表达式(或return语句)为止。换言之,Generator 函数是分段执行的,yield表达式是暂停执行的标记,而next方法能够恢复执行spa
function* helloWorldGenerator() { yield 'hello'; yield 'world'; return 'ending'; } var hw = helloWorldGenerator(); hw.next() // { value: 'hello', done: false } hw.next() // { value: 'world', done: false } hw.next() // { value: 'ending', done: true } hw.next() // { value: undefined, done: true }
调用 Generator 函数,返回一个遍历器对象,表明 Generator 函数的内部指针。之后,每次调用遍历器对象的next方法,就会返回一个有着value和done两个属性的对象。value属性表示当前的内部状态的值,是yield表达式后面那个表达式的值;done属性是一个布尔值,表示是否遍历结束。prototype
yield表达式与return语句既有类似之处,也有区别。类似之处在于,都能返回紧跟在语句后面的那个表达式的值。区别在于每次遇到yield,函数暂停执行,下一次再从该位置继续向后执行,而return语句不具有位置记忆的功能。一个函数里面,只能执行一次(或者说一个)return语句,可是能够执行屡次(或者说多个)yield表达式。正常函数只能返回一个值,由于只能执行一次return;Generator 函数能够返回一系列的值,由于能够有任意多个yield。从另外一个角度看,也能够说 Generator 生成了一系列的值,这也就是它的名称的来历(英语中,generator 这个词是“生成器”的意思)。代理
语法注意点:
1.yield表达式只能用在 Generator 函数里面
2.yield表达式若是用在另外一个表达式之中,必须放在圆括号里面
3.yield表达式用做函数参数或放在赋值表达式的右边,能够不加括号。
例如:指针
function* demo() { foo(yield 'a', yield 'b'); // OK let input = yield; // OK }
yield表达式自己没有返回值(就是说let a=yield ;会返回undefined),或者说老是返回undefined。next方法能够带一个参数,该参数就会被看成上一个yield表达式的返回值 (注意,是整个表达式的返回值而不仅是yield 后方的值,例如 let a=yield.......... 参数会是a 的值而且会覆盖表达式以前的值)。code
function* f() { for(var i = 0; true; i++) { var reset = yield i; console.log(reset); if(reset) { i = -1; } } } var g = f(); g.next()
因为next方法的参数表示上一个yield表达式的返回值,因此在第一次使用next方法时,传递参数是无效的。V8 引擎直接忽略第一次使用next方法时的参数,只有从第二次使用next方法开始,参数才是有效的。从语义上讲,第一个next方法用来启动遍历器对象,因此不用带有参数。对象
Generator 函数返回的遍历器对象,都有一个throw方法,能够在函数体外抛出错误,而后在 Generator 函数体内捕获。blog
var g = function* () { try { yield; } catch (e) { console.log('内部捕获到错误', e); } }; var i = g(); i.next(); //外部抛出错误: i.throw('a');
注意点:
1.throw方法抛出的错误要被内部捕获,前提是必须至少执行过一次next方法。
2.throw方法被捕获之后,会附带执行下一条yield表达式。也就是说,会附带执行一次next方法。
3.Generator 函数体外抛出的错误,能够在函数体内捕获;反过来,Generator 函数体内抛出的错误,也能够被函数体外的catch捕获。
4.一旦 Generator 执行过程当中抛出错误,且没有被内部捕获,就不会再执行下去了。若是此后还调用next方法,将返回一个value属性等于undefined、done属性等于true的对象,即 JavaScript 引擎认为这个 Generator 已经运行结束了。继承
Generator 函数返回的遍历器对象,还有一个return方法,能够返回给定的值,而且终结遍历 Generator 函数。
语法角度看,若是yield表达式后面跟的是一个遍历器对象,须要在yield表达式后面加上星号,代表它返回的是一个遍历器对象。这被称为yield表达式(我的理解yield 主要用做遍历具备遍历器(Iterator)接口的对象或函数)。
如:
用来在一个 Generator 函数里面执行另外一个 Generator 函数。
function* foo() { yield 'a'; yield 'b'; } // 直接调用没有效果 function* bar() { yield 'x'; foo(); yield 'y'; } // 使用yield* foo(); 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); }
若是被代理的 Generator 函数有return语句,那么就能够向代理它的 Generator 函数返回数据。
function* foo() { yield 2; yield 3; return "foo"; } function* bar() { yield 1; var v = yield* foo(); console.log("v: " + v); yield 4; } var it = bar(); it.next() // {value: 1, done: false} it.next() // {value: 2, done: false} it.next() // {value: 3, done: false} it.next(); // "v: foo" // {value: 4, done: false} it.next() // {value: undefined, done: true}
若是一个对象的属性是 Generator 函数,能够简写成下面的形式。
let obj = { * myGeneratorMethod() { ··· } };
完整形式
let obj = { myGeneratorMethod: function* () { // ··· } };
Generator 函数老是返回一个遍历器,ES6 规定这个遍历器是 Generator 函数的实例,也继承了 Generator 函数的prototype对象上的方法。
function* g() {} g.prototype.hello = function () { return 'hi!'; }; let obj = g(); obj instanceof g // true obj.hello() // 'hi!'
上面代码代表,Generator 函数g返回的遍历器obj,是g的实例,并且继承了g.prototype。可是,若是把g看成普通的构造函数,并不会生效,由于g返回的老是遍历器对象,而不是this对象。
function* g() { this.a = 11; } let obj = g(); obj.next(); obj.a // undefined
Generator 函数也不能直接跟new命令一块儿用
function* F() { yield this.x = 2; yield this.y = 3; } new F()
变通方法
function* gen() { this.a = 1; yield this.b = 2; yield this.c = 3; } function F() { return gen.call(gen.prototype); } var f = new F(); f.next(); // Object {value: 2, done: false} f.next(); // Object {value: 3, done: false} f.next(); // Object {value: undefined, done: true} f.a // 1 f.b // 2 f.c // 3