Javascript Event Emitter With your Barehand (Node.js)
I just want to show you very simple code to explain how does EventEmitter works in Node.js.
>$ var event = require('events').EventEmitter;
undefined
//create new EventEmitter object
>$ var greeting = new event();
undefined
// registering new event using keyword `on` with callback function and params
>$ greeting.on('sayHello', function(person) {
... console.log(`hello ${person}!`);
... }
... );
EventEmitter {
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { sayHello: [Function] },
_eventsCount: 1,
_maxListeners: undefined }
//calling Event using keyword `emit` with parameter to be passed into callback function
>$ greeting.emit('sayHello', 'Jono');
hello Jono!
true