views:

978

answers:

4

EDIT: OK, I believe the following solutions are valid:

  1. Use the jQuery AOP plugin. It basically wraps the old function together with the hook into a function sandwich and reassigns it to the old function name. This causes nesting of functions with each new added hook.
    If jQuery is not usable for you, just pillage the source code, there did not seem to be any jQuery dependencies in the plugin, and the source is simple and very small.

  2. Have an object describing all hooks and their targets and one to store the initial unmodified function. When adding a new hook, the wrapping would be redone around the original function, instead of re-wrap the the previous wrapping function.
    You escape nested functions, and get two objects to handle instead. Potentially, this could also mean easier hook handling, if you add/remove hooks often and out of order.

I'll go with the first, since it's already done, and I don't have performance to worry about. And since the original functions are not affected, even if I switch hooking methods, I'll only need to redo the hook adding, which might be just some simple search&replace operations.


Hi,

Is it possible to create a mechanism, in which function A might have a set of hooks(functions that will execute before/after function A)?

Ideally, function A would not be aware of hooking functionality, so that I do not have to modify the source code of function A to call the hooks. Something like:

A = function(){
    alert("I'm a naive function");
};
B = function(){
    alert("I'm having a piggyback ride on function A!"+
          "And the fool doesn't even know it!");
};
addHook(B, A)//add hook B to function A
A()
//getting alerts "I'm a naive function"/"I'm having a 
//piggyback ride on function A! And the fool doesn't even know it!"

I've been trying to hack something up for a couple of hours, but so far no luck.

+6  A: 

Take a look at jQuery's AOP plugin. In general, google "javascript aspect oriented programming".

Travis Jensen
jQuery != Javascript. This matters for instance, if you are programming on the Facebook platform, and probably other "sandboxed" environments as well.
George Jempty
@George: the techniques demonstrated still apply. So far, Greg is the only one to post usable code *here*.
Shog9
Thank you for the link and mention of AOP, i wasn't familiar with it. I am able and do use jQuery, so this might work out. Trying to figure out the plugin right now.
Seyen
+6  A: 

Might not be pretty but it seems to work...

<script>

function A(x) { alert(x); return x; }
function B() { alert(123); }

function addHook(functionB, functionA, parent)
{
    if (typeof parent == 'undefined')
     parent = window;

    for (var i in parent)
    {
     if (parent[i] === functionA)
     {
      parent[i] = function()
      {
       functionB();
       return functionA.apply(this, arguments)
      }

      break;
     }
    }
}

addHook(B, A);

A(2);

</script>
Greg
Thanks! I thought about this one, and it works, but you get nesting with this method, since each new hook wraps all previous hooks: hookC(hookB(hookA(functionA.apply(this,arguments)))) I'm not sure if this affects performance in any way, but it doesn't feel right/safe. Is `parent` used so that this from `apply(this, arguments)` is bound correctly?
Seyen
A: 

This answer is not definitive, but rather demonstrative of a different technique than those offered thus far. This leverages the fact that a function in Javascript is a first-class object, and as such, a) you can pass it as a value to another function and b) you can add properties to it. Combine these traits with function's built-in "call" (or "apply") methods, and you have yourself a start toward a solution.

var function_itself = function() {
    alert('in function itself');
}
function_itself.PRE_PROCESS = function() {
    alert('in pre_process');
}
function_itself.POST_PROCESS = function() {
    alert('in post_process');
}

var function_processor = function(func) {
    if (func.PRE_PROCESS) {
        func.PRE_PROCESS.call();
    }
    func.call();
    if (func.POST_PROCESS) {
        func.POST_PROCESS.call();
    }        
}
George Jempty
This works, but then you have to use function_processor for calling function_itself in all places where it would be called, which somewhat defeats the purpose. The code might quickly devolve into a forest of function_processor calls.
Seyen
A: 

Very simple answer:

function someFunction() { alert("Bar!") }
var placeholder=someFunction;
someFunction=function() {
  alert("Foo?");
  placeholder();
}
Asmor