2

How can i overload the calling process of any function?

In my web app i want to do something before the call of a function and something after it how can i do this without prototyping the call method ( because i tried this and it will work only if i call a function as myFunction.call() ) and still have the requested effect.

I have tried everything for making it work, but nothing works, and to do it the hard way ( by call method ) it's non-practicable because i would have to rewrite all my code.

Could someone help please?

khael
  • 2,600
  • 1
  • 15
  • 36
  • possible duplicate of [Is it possible to override the "call" function?](http://stackoverflow.com/questions/7329923/is-it-possible-to-override-the-call-function) – pimvdb Dec 26 '11 at 14:29
  • I don't think you are using the term 'overload' in the correct meaning. Anyway, are you asking for this? http://stackoverflow.com/questions/676721/calling-dynamic-function-with-dynamic-parameters-in-javascript – Tomislav Markovski Dec 26 '11 at 14:30
  • @pimvdb it is not a duplicate but is somewhat alike. What i try to do is not to override the call method but the built in process of calling functions to do exactly what i've said. The problem is that if i override the call method it will only work by calling a function through the overridden call function and if i do that i have to change tons of code because i need every single function to be automatically called with the set before and after so called events. – khael Dec 29 '11 at 18:06

2 Answers2

3

You can change each function definition manually.
You can change each function call manually.
If either of these refactorings is out of the scope of your problems then you're in a spot of bother.

There is no generic way that I'm familiar with to solve your problem.

However if your functions are globally accessible or namespaced then you can do the following quite easily (and can make it much more generic by parametrising the pre and post functions etc.):

NS = {
   foo : function(){ console.log('foo'); },
   bar : function(){ console.log('bar'); }
};

// <-- new code goes here

NS.foo();
NS.bar();

// new code below, that should go after definitions but before the calls

(function(){

   var pre = function(){ console.log('pre'); },
       post = function(){ console.log('post'); };

   for (var fn in NS) {
      NS[fn] = (function(fn){ return function(){ pre(); fn(); post(); }; })(fn);
   }

})();
davin
  • 44,863
  • 9
  • 78
  • 78
  • It might be a good idea to enable context/arguments by passing `this` and `arguments` along using `.apply`. – pimvdb Dec 26 '11 at 14:51
  • @pimvdb, yeah that's what I meant by "it's easy to make it more generic". – davin Dec 26 '11 at 15:11
3

You can create a caller function that accept the function name, the parammeters and the context as parameters:

function pre() {
    alert("I'm before the call");
}

function post() {
    alert("I'm after the call");
}


function caller(func, parameters, context) {
    pre();

    func.apply(context, func, parameters.split(','));

    post();
}

Or use AnthonyWJones solution on Calling dynamic function with dynamic parameters in Javascript that can be called this way caller(funcName, param1, param2);:

function caller(func){
    pre();

    this[func].apply(this, Array.prototype.slice.call(arguments, 1)); 

    post();
} 
Community
  • 1
  • 1
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69