在使用 ES6 类 和生成器函数时,如何避免使用 `self = this` hack?

How do I get away from using the `self = this` hack when using ES6 classes and generator functions?

我试过使用显式 .bind(this) 但没有用。我也知道箭头函数在这里不起作用。

'use strict';

const co     = require('co');

class ServiceDemo {

    constructor(repository, config, loggingService) {
        this.config = config;
        this.repository = repository;
        this.loggingService = loggingService;
    }

    checkForNotifications(pricePoint) {

        const self = this;

        return co(function*() {
            self.loggingService.debug('test');
            //const surprisesToNotify = yield this.getSomething(pricePoint);
        });
    }

    getSomething(){
        return co(function*() {
            return {};
        });
    }

}

module.exports = SurpriseSchedulerService;

使用 .bind(this) 应该有效:

(function() {
  return this === function*() {
    return this; // global object or undefined
  }().next().value;
}).call({}); // false :(
(function() {
  return this === function*() {
    return this; // outer this
  }.bind(this)().next().value;
}).call({}); // true :)

co 将在调用生成器时使用它所调用的上下文:

co.call( this, function*() {
    this.loggingService.debug('test');
});