Events and the Event Emitter
Building Our Own Event Emitter
function Emitter() {
this.events = [];
}
Emitter.prototype.on = function(type, listener) {
// check if existing
this.events[type] = this.events[type] || [];
// add to container
this.events[type].push(listener);
};
Emitter.prototype.emit = function(type) {
if (this.events[type]) {
this.events[type].forEach(function(listener) {
listener();
})
}
}
module.exports = Emitter;The Node Event Emitter
Using the Event Emitter
Inheriting From the Event Emitter
Last updated