Skip to content

Instantly share code, notes, and snippets.

@azproduction
Last active December 16, 2015 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azproduction/5425673 to your computer and use it in GitHub Desktop.
Save azproduction/5425673 to your computer and use it in GitHub Desktop.
yet another EventManager
function EventManager () {
this._events = {};
}
EventManager.prototype = {
_event: function (type) {
var events = this._events[type];
if (!events) {
events = this._events[type] = {};
}
return events;
},
on: function (type, listener){
this._event(type).push(listener);
},
trigger: function (type, data) {
var event = {
type: type,
target: this
};
var events = this._event(type);
for (var i = 0, c = events.length; i < c; i++) {
try {
events[i](event, data);
} catch (e) {}
}
},
off: function (type, listener) {
var events = this._event(type),
index = events.indexOf(listener);
if (index < 0) {
return;
}
events.splice(index, 1);
}
};
@surr-name
Copy link

в 10-й строке {} это опечатка? разве не []?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment