I wrote a callback helper, that lets me group multiple callbacks into one function variable:
function chainCallbacks() {
var callbacks = arguments;
return function () {
for(var i = 0; i < callbacks.length; i++) {
if(callbacks[i] != null) {
callbacks[i].apply(null, arguments);
}
}
};
}
this works, but I'm wondering if there are any javascript libraries that provide the same functionality? or even better, something that simulates the .NET "event" pattern?
myEvent+=myCallback;
I have modified your chainCallbacks function. You can test below code in JS console (I'm using Chrome -works fine), and check the result.
Idea is simple - you call
_next()
whenever your callback function has finished executing, and you want to jump to another. So you can call_next()
e.g. after some jQuery animation as well, and this way you will preserve the order of the functions.If you want to replace a callback with one that calls the original as well as some others, I'd probably just do something like this:
But if you're doing this often, I think your solution will be as good as it gets. I don't see need for a library.
A library can't do anything to extend language syntax in JavaScript. It's limited to what's available... no operator overloading or anything.