通常,在“构造函数”中,您使用 lambda 函数订阅事件:
function Something(){
this.on('message', function(){ ... });
}
util.inherits(Something, events.EventEmitter);
这很好用,但效果不好。方法更适合继承:
function Something(){
this.on('message', this._onMessage);
}
util.inherits(Something, events.EventEmitter);
Something.prototype._onMessage = function(){ ... };
保留这些事件处理函数的最佳实践是什么?