Skip to content

Instantly share code, notes, and snippets.

@petvas
Created April 29, 2016 21:30
Show Gist options
  • Save petvas/74484ad334a68f591955819f35f86d6e to your computer and use it in GitHub Desktop.
Save petvas/74484ad334a68f591955819f35f86d6e to your computer and use it in GitHub Desktop.
class less Event Emitter in 20 lines
function eventEmitter() {
var events = {};
return {
bind: function bind(name, cb) {
events[name] = events[name] || [];
events[name].push(cb);
},
unBind: function unBind(name, cb) {
if (!events[name]) return;
events[name].splice(events[name].indexOf(cb), 1);
},
trigger: function trigger(name) {
if (!events[name]) return;
var args = arguments;
events[name].forEach(function (cb) {
cb.apply(null, Array.prototype.slice.call(args, 1));
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment