1const EventEmitter = require('events');
2
3class MyEmitter extends EventEmitter {
4 foo() {
5 this.emit('test');
6 }
7}
8const myEmitter = new MyEmitter();
9myEmitter.on('test', () => console.log('Yay, it works!'));
10
11myEmitter.foo(); // Yay it works!
1const myEmitter = new MyEmitter();
2let m = 0;
3myEmitter.on('event', () => {
4 console.log(++m);
5});
6myEmitter.emit('event');
7// Prints: 1
8myEmitter.emit('event');
9// Prints: 2