Last active
November 1, 2016 03:30
-
-
Save enijar/c860dfda7b95a0df92f8929305d6d566 to your computer and use it in GitHub Desktop.
JavaScript Event System
This file contains 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 Event = { | |
events: {}, | |
on(event, func) { | |
if (!this.events.hasOwnProperty(event)) { | |
this.events[event] = []; | |
} | |
this.events[event].push(func); | |
}, | |
fire(event, data) { | |
if (!this.events.hasOwnProperty(event)) { | |
return; | |
} | |
this.events[event].map(func => func(data)); | |
}, | |
off(event) { | |
if (!this.events.hasOwnProperty(event)) { | |
return; | |
} | |
this.events[event].splice(0, this.events[event].length - 1); | |
if (this.events[event].length === 0) { | |
delete this.events[event]; | |
} | |
} | |
}; | |
export default Event; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: