Created
March 21, 2018 20:23
-
-
Save aaroncadrian/5ae50bd4ab19e24f9b1b017df4d51035 to your computer and use it in GitHub Desktop.
Simple JavaScript event emitter class
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(eventName, callback) { | |
| if(!this.events[eventName]) { | |
| this.events[eventName] = []; | |
| } | |
| this.events[eventName].push(callback); | |
| }; | |
| trigger(eventName, ...args) { | |
| if(this.events[eventName]) { | |
| this.events[eventName].forEach(callback => callback.apply(null, args)); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment