Last active
January 15, 2018 10:31
-
-
Save afterburn/a5ae55f373e77eca2325cb6b0be65f60 to your computer and use it in GitHub Desktop.
Simple EventEmitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventEmitter { | |
constructor () { | |
this.events = {} | |
} | |
on (event, callback) { | |
if (!this.events.hasOwnProperty(event)) { | |
this.events[event] = [] | |
} | |
this.events[event].push(callback) | |
} | |
call () { | |
const event = arguments[0] | |
const newargs = [...arguments].splice(0).slice(1, arguments.length) | |
if (this.events.hasOwnProperty(event)) { | |
this.events[event].forEach(callback => callback.bind(null, ...newargs)()) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Events = new EventEmitter() | |
Events.on('ready', function() { | |
console.log('ready event called') | |
}) | |
Events.call('ready') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment