1

What I want to do is to execute a function automatically every time BEFORE ANY function is executed in JS, regardless if it's a custom or native function if possible.

ie.

    whatIWant(functionName){
      return console.log('called before '+functionName);
    } 

    function blah(){
      return console.log('called blah');
    }

    function meh(){
      return console.log('called meh');
    }

    alert('woot');


    blah();
    //will output :
    //called before blah
    //called blah

    meh();
    //will output :
    //called before meh
    //called meh

    alert();
    //will output :
    //called before alert
    //will pop up dialog: woot

I do not want to do the following:

    Function.prototype.onBefore = function(){};

    blah.onBefore();

is it even possible to do what I am asking for? any suggestions, read, or w/e?

Thanks in advance.

R.D.
  • 1,557
  • 2
  • 10
  • 11
  • I guess just parsing your script uses some native function(s), so yours can't be the first one... The closest I can imagine is to use an [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/). – Teemu Oct 26 '13 at 18:43
  • Maybe if you describe what the real problem is that you're trying to solve, we could know better how to answer. Trying to modify built-in prototypes before any objects are created is not a wise thing to base any solution on. – jfriend00 Oct 26 '13 at 18:47
  • The real question is... why? – plalx Oct 26 '13 at 18:48
  • Thanks for your answers, I'll take a look at IIFE meanwhile. The question arised from a set of exercises I am doing to learn 'advanced' JS. The exercise is: "create a function that can limit the execution of other functions to a determined amount of times". – R.D. Oct 26 '13 at 19:01
  • @R.D. for that exercise, I think you should think about a function that you can call, passing it a function and a count, and the function *returns* a function that does what the input function does but only until the invocation count is reached. – Pointy Oct 26 '13 at 19:23
  • @Pointy @jj689 oh my... I guess I'm still sleepy as I didn't read the example: `code` function limitFunc(fnName, limitNum) { ... } var limited = limitFunc( fnName, 2); limited(); // executes fine limited(); // executes fine limited(); // does not execute `code` so, I think I got the answer with @jj689 answer and @Pointy suggestion... of course, @Teemu 's IIFE docs will also help. I'll work on it and post the result. Thanks!! – R.D. Oct 26 '13 at 19:39

2 Answers2

1

What about just providing your function as a callback to whatIWant like this:

function whatIWant(fn) {
    var fnName = fn.toString();
    fnName = fnName.substr('function '.length);
    fnName = fnName.substr(0, fnName.indexOf('('));
    console.log('called before ' + fnName);
    fn();
}

function meh() {
    console.log('called meh');
}

function blah() {
    console.log('called blah');
}

whatIWant(meh);

whatIWant(blah);

whatIWant(alert)
jj689
  • 456
  • 2
  • 4
1

what do you guys think about this solution? :)

  function bleh(){
    console.log('exe a');
  }

  function limitFn(fn,n) {
      var limit = n ;
      var counter = 1 ;
      var fnName = fn.toString();
      fnName = fnName.substr('function '.length);
      fnName = fnName.substr(0, fnName.indexOf('('));
      return function(){
        if(counter <= limit) {
          console.log(counter + ' call before ' + fnName + ' limit ' + limit);
          counter++;
          fn();
        } else {
          console.log('limit of ' + limit + ' exes reached') ;
        }
      };
  }



  limited = limitFn(bleh,2);

  limited();
  limited();
  limited();
  limited();
R.D.
  • 1,557
  • 2
  • 10
  • 11