admin管理员组文章数量:1391017
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
1 Answer
Reset to default 10Judging 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) { ... }
}
本文标签: javascriptMigrating EventEmitter to ES6Stack Overflow
版权声明:本文标题:javascript - Migrating EventEmitter to ES6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744623737a2616187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论