var f = function() {
// Do something useful here
};
Is there a way to ‘observe’ this function, and get notified when it is executed? Something similar to bind in jQuery, but I want to bind functions and not dom events?
I don’t want something like this:
var f = function() {
// Do something useful here
notifyObserver();
};
but I want something like this:
f.bind(function() {
alert('F was executed.');
});
You could replace f with a function that calls notifyObserver:
That way you don’t need to modify (the old) f itself. This doesn’t include your
bindfunctionality, of course. I’d probably create some kind of manager class for this where you can register event handlersAnd creating the wrapper function would look more like
You can generalize the creation of the wrapper:
(This works with any number of arguments and return values!)
And then do something like:
See: http://jsfiddle.net/NBefc/2/ (updated, no with return values)