1

Is it possible to execute something every time a specific function runs without any knowledge about that function besides its name?

This would be similar to bind

var clicked = 0;
$('#foo').bind('click',function(){
    clicked += 1;
    alert(clicked);
});

So there, every time something with the ID foo is clicked, it will add 1 to the variable clicked so that I know how many times it has been clicked. What I want to do would be the equivalent of the following if it were correct syntax:

var fired = 0;
$('my_function').bind('run',function(){
        fired += 1;
        alert(fired);
});

I don't care if in any given situation you would be in, you would always be able to figure something out about the function and use that, I don't want work arounds, this is what I want for an answer:

How I can execute something everytime a specific function runs, just given the name of the function. If that is not possible, why not?

3
  • 1
    Let answerers know that you are aware of one possible solution stackoverflow.com/questions/17202346/…
    – compid
    Commented Jun 19, 2013 at 22:53
  • How is this question different to your last question (linked by compid above)?
    – Paul S.
    Commented Jun 19, 2013 at 23:05
  • As I realize now, this one should really be closed. I thought they were different, but they aren't. The difference is the other has a lot of irrelevant information and this one is very clear and to the point. But because people answered immediately, I could not delete this one.
    – Ryan Saxe
    Commented Jun 19, 2013 at 23:12

5 Answers 5

2

Try something like this:

var temp = my_function, fired = 0;
my_function = function() {
    fired++;
    temp.apply(this,arguments);
}
1

I think something like this may be the closest you can come:

function adjustFunctionToCount(f){
    var count = 0;
    function newF(){
        count++;
        f.apply(this, arguments);
    }
    newF.getCount = function(){ return count; };
    return newF;
}

And so if you have

function handler(val){
    console.log('called with val ' + val);
}

You could do

handler = adjustFunctionToCount(handler);
handler('a');
handler('b');
console.log(handler.getCount());

FIDDLE

And needless to say you could create your function inline

var handler = adjustFunctionToCount(function(val){ console.log('called with val ' + val); });

handler('a');
handler('b');
console.log(handler.getCount());

UPDATED FIDDLE

0
0

I'm pretty sure that's impossible in the general case.

Remember, functions are objects, really, and the name of a function is just a variable. Functions can exist without being assigned to a named variable, the variables can be out of your scope, or reassigned/swapped around. In any case, I know of no API that lets you hook onto a JS function call.

This may be of interest: Can I intercept a function called directly?

0

This is where event driven programming comes in - and jQuery makes it really easy to do.

var myFunction = function() {
 //...
 //...
 //...
 $(document).trigger('someEvent');

}


$(document).on('someEvent',function() {
 //the function you would like to run every time myFunction is called
});
0

Try this:

var count = (function(){
  var c = 0;
  return function(){
    alert(c++);
  };
})();
$('#foo').click(count);

OR

$('#foo').bind('click', count);

When an Anonymous Function or a Variable that represents a Function is passed it is the same thing. You could make your own code that executes a Function like this:

function executeFun(func){
  return func();
}
executeFun(count)
executeFun(function(){
  /*everything happens in here. The Anonymous Function will be called
    automatically because of the parameter next to the variable func above */
})

Although, that example is impractical it shows you what happens internally. Also, I solved your potential global scope variable problem with a Closure. For more on Closures visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures .

Not the answer you're looking for? Browse other questions tagged or ask your own question.