Ok, so I'm working a project where it is helpful to have custom events. This is how I'm doing it.
Code:
var hooks = {//Custom events object
hooks: {},//Store an event handler
add: function(hook,func){
//Add function func to handle hook
var oldhook = this.hooks[hook] || false;
this.hooks[hook] = oldhook ? function(data){oldhook(data);func(data);} : func;
},
call: function(hook,data){
//Call a hook
this.hooks[hook] && this.hooks[hook](data);
}
};
It works, but I keep thinking the use of closures here is a bad idea. Does anyone know what kind of performance hit this code would have?
Bookmarks