i need to “detect” when some third party function returns and then execute my own function.
example: some function i didn’t write
function foo(params1){
console.log(params1);
}
my function:
function bar(params2){
console.log(params2);
}
i need that whenever foo is called, it somehow calls bar immediately !
i tried going this way:
foo = function(params1){
console.log(params1);
bar(params2);
}
meaning i “overwrite” foo .. but sometimes (always), foo code is too lengthy + passing params2 (minor) problem.
I tried desperate attempts with prototype and so .. but then i figured i need some support on this.
i also tried this: (but)
var tmp = foo;
foo = function(params1) {
tmp(params1);
bar(params2);
}
Same problem here, the caller will call foo(params1) thus bar won’t “find” params2.
I need this to make something like an “event handler”.
The third party function makes some ajax calls, acts on the DOM, i immediately take action.
Is there a neat way of doing this? i hope this is interesting. Thanks.
ps: i though about making params2 global, but i still need an “original” solution 🙂
As for the use of params2, let’s say i want to use different params for every function i want to hook.
examples:
var tmp = foo_1;
foo_1 = function(params1) {
tmp(params1);
bar(params2);
}
tmp = foo_2;
foo_2 = function(params1) {
tmp(params1);
bar(params3);
}
//etc..
Maybe you should use a simple pubSub system to get notified when foo is called. This will decouple your call of
barfromfoo.