Skip to content

Instantly share code, notes, and snippets.

@RahulJyala7
Created July 27, 2021 06:04
Show Gist options
  • Save RahulJyala7/f3eea00b9caf945e0785c8d0334bd072 to your computer and use it in GitHub Desktop.
Save RahulJyala7/f3eea00b9caf945e0785c8d0334bd072 to your computer and use it in GitHub Desktop.
CustomEmitter
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