最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Migrating EventEmitter to ES6 - Stack Overflow

programmeradmin2浏览0评论

I'm getting an error that getToken is undefined when called as below and after transpiling with gulp-babel. Moving the constructor to bottom of class does not help either. Can anyone advise?

I think it has something to do with the util inheriting, which may be trying to take ES5 code and apply it to an area where ES6 does things very differently?

var events = require('events');
var util = require('util');
class Report {

    constructor(private_key, service_email, debug) {
        this.private_key = private_key;
        this.service_email = service_email;
        this.debug = debug || false;

        events.EventEmitter.call(this);

        this.getToken( (err, token) => {
            if (err) throw err;

            return this.emit('ready');
        });
    }

    getToken(cb) {
        ...
    }
}

util.inherits(Report, events.EventEmitter);

module.exports = Report;

I'm getting an error that getToken is undefined when called as below and after transpiling with gulp-babel. Moving the constructor to bottom of class does not help either. Can anyone advise?

I think it has something to do with the util inheriting, which may be trying to take ES5 code and apply it to an area where ES6 does things very differently?

var events = require('events');
var util = require('util');
class Report {

    constructor(private_key, service_email, debug) {
        this.private_key = private_key;
        this.service_email = service_email;
        this.debug = debug || false;

        events.EventEmitter.call(this);

        this.getToken( (err, token) => {
            if (err) throw err;

            return this.emit('ready');
        });
    }

    getToken(cb) {
        ...
    }
}

util.inherits(Report, events.EventEmitter);

module.exports = Report;
Share Improve this question edited Sep 18, 2016 at 9:19 Simon H asked Jul 1, 2015 at 19:49 Simon HSimon H 21.1k14 gold badges81 silver badges142 bronze badges 7
  • curious: does putting the constructor physically at the bottom help? – dandavis Commented Jul 1, 2015 at 19:52
  • Can you show a plete code? (Verifiable..) – Amit Commented Jul 1, 2015 at 19:54
  • Works fine for me ([email protected]). – robertklep Commented Jul 1, 2015 at 19:55
  • @dandavis no, thanks for the idea though – Simon H Commented Jul 1, 2015 at 19:57
  • 1 People should really read How to Ask. Save the world a lot of time – Amit Commented Jul 1, 2015 at 20:05
 |  Show 2 more ments

1 Answer 1

Reset to default 10

Judging by the call to events.EventEmitter in your constructor, you probably also using this:

require('util').inherits(Report, events.EventEmitter);

This breaks the Report class (not sure why, but I can reproduce the problem).

Instead, use ES6-style inheritance:

class Report extends events.EventEmitter {

  constructor(private_key, service_email, debug) {
    super();
    ...
  } 

  getToken(cb) { ... }
}
发布评论

评论列表(0)

  1. 暂无评论