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

我试图使用明确的.bind(这),并没有工作。 我也知道箭头function不在这里工作。

'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; 

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

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

使用.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 :)