You can not register the listeners separately for each instance, and the natural place for this is in the constructor 1, 2 . However, you can not create new listening functions:
class Cat extends EventEmitter { constructor() { super(); this.on('wave', this.onWave); } onWave() { console.log('prototype wave'); } } var cat = new Cat(); cat.emit('wave');
1: There are other ways such as getter for ._events . You can do all kinds of fancy things with this, including prototype inheritance for listeners by default, but they are completely complex and quickly cope with your head. You can also do fancy things - and much cleaner - just by putting some common code in the constructor.
2: You can also override (specialize) the init method for EventEmitters, but it comes down to the same.
Bergi
source share