title: Javascript function hooks
body: <p>EDIT: OK, I believe the following solutions are valid:</p>
  
  <ol>
  <li><p>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.<br>
  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.</p></li>
  <li><p>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.<br>
  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.</p></li>
  </ol>
  
  <p>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&amp;replace operations.</p>
  
  <hr>
  
  <p>Hi,</p>
  
  <p>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)?</p>
  
  <p>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:</p>
  
  <pre><code>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!"
  </code></pre>
  
  <p>I've been trying to hack something up for a couple of hours, but so far no luck.</p>