Generator 的 this

  1. function* hello(){
  2. this.name = "world";
  3. this.say = function(){
  4. return this; // otherThis
  5. }
  6. return this; // oneThis
  7. }
  8. let gen = hello.call(hello.prototype); // 必须
  9. let oneThis = gen.next().value;
  10. let otherThis = gen.say();
  11. oneThis === otherThis // false
  12. otherThis === gen // true
  13. oneThis === otherThis.__proto__ // true
  14. oneThis === hello.prototype // true
  15. otherThis instanceof hello // true
  16. oneThis.name // world
  17. otherThis.name // world

由上代码可以看出两个各方式返回的this并不是都代表hello函数的实例;其中oneThis指向hello函数的原型hello.prototype,otherThis指向hello的实例