Created
July 27, 2021 06:04
-
-
Save RahulJyala7/f3eea00b9caf945e0785c8d0334bd072 to your computer and use it in GitHub Desktop.
CustomEmitter
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
function CustomEmitter() { | |
this.events = {}; | |
} | |
CustomEmitter.prototype.on = function (type, listener) { | |
// error handling | |
this.events[type] = []; | |
this.events[type].push(listener); | |
}; | |
CustomEmitter.prototype.emit = function (type, value) { | |
if (this.events[type]) { | |
this.events[type].forEach(function (listener) { | |
listener(value); | |
}); | |
} | |
}; | |
const emit = new CustomEmitter(); | |
emit.on("mess", (data) => {console.log(data)}) | |
emit.emit("mess", "hiiii"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment