1

I have written a program with lots of functions and function calls. I would like to be able to call code just before every function call. For example, each time a function is called, have the console log

console.log("A function has been called!");

I would like to write this once and for all, not go through every function declaration. I guess this is similar in spirit to the CSS pseudo elements :before and :after, but applied to function calls in JavaScript.

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 1
    Is this just for debugging or do you have some deeper purpose? – hugomg Sep 20 '11 at 17:44
  • I want to inject 1ms pauses at every function invocation to allow for DOM repaints to happen smoothly. – Randomblue Sep 20 '11 at 17:55
  • 1
    Javascript doesn't have a sleep function that would allow you to easily do that; not *every* function would "need" to be changed (only the repainting ones) and finally, this just sounds too hacky - there are more standard ways to solve this king of problem. – hugomg Sep 20 '11 at 18:08
  • Agreed with @missingno; this sounds like a fairly awful idea. You might make DOM repaints "smoother" but this could very well bring JS execution down to a grinding halt, by adding a 1 ms overhead to every function call. – Matt Ball Sep 20 '11 at 18:35
  • To my knowledge, console.log() doesn't free the browser thread from executing Javascript. Instead, you should use setTimeout() in strategic locations to get the desired effect, but definitely not in the beginning of every function. – zatatatata Sep 20 '11 at 19:05

5 Answers5

3

I don’t think there’s any built-in way to do something like this. Why not use a JavaScript debugger like Firebug and set breakpoints at the functions you think might be giving you trouble?

bdesham
  • 15,430
  • 13
  • 79
  • 123
2

You're basically looking for Aspect-Oriented Programming in JavaScript. See Javascript AOP libraries.

More questions: https://stackoverflow.com/questions/tagged/javascript+aop

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

I don't know how to automatically inject console spew for every function. But if you want to be explicit about which functions spew logging to the console, you could likely use a closure.

function ConsoleLogWrap(obj, funcname, msg) {
    var xmsg = msg;
    var xfuncname = funcname;
    var xobj = obj;

    obj[funcname+"$"] = obj[funcname];
    obj[funcname] = function () {

        try {
            console.log(xmsg);
        } catch(e) {
        }

        xobj[xfuncname+"$"](arguments);

    };

}


function Addition(x, y) {
   return x + y;
}

ConsoleLogWrap(window, "Addition", "Addition was called");

window.Addition(5,5); // this will spew out "Addition was called" and return the result of 5+5.
selbie
  • 100,020
  • 15
  • 103
  • 173
1

You can add methods to every function you create by extending Function.prototype.

To make a function run asynchronously as you seem to indicate in your comment, you could add a function like this:

Function.prototype.makeAsync = function() {
    var func = this;
    var args = arguments;
    setTimeout(function() {
        func.call.apply( func, args );
    }, 1 );
};

...then invoke it like this:

function my_func( a, b, c ) {
    console.log( this );
    console.log( a, b, c );
}

my_func.makeAsync( element, 1, 2, 3 );

Note that the first argument you pass to makeAsync becomes the this value of the function you're invoking.

DEMO: http://jsfiddle.net/3kuad/

user113716
  • 318,772
  • 63
  • 451
  • 440
-1

I've never seen anyone do this before, so I'm assuming it's not possible.

I did a quick google search and nothing useful came back. Just inheritance, but I don't see how that's particularly helpful.

Looks like bdesham beat me to it!

James
  • 5,137
  • 5
  • 40
  • 80