Created
April 29, 2016 21:30
-
-
Save petvas/74484ad334a68f591955819f35f86d6e to your computer and use it in GitHub Desktop.
class less Event Emitter in 20 lines
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
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