title: Wrapping a function in Javascript / jQuery
body: <p>If I have an arbitrary function <code>myFunc</code>, what I'm aiming to do is replace this function with a wrapped call that runs code before and after it executes, e.g.</p>
  
  <pre><code>// note: psuedo-javascript
  
  var beforeExecute = function() { ... }
  var afterExecute = function() { ... }
  
  myFunc = wrap(myFunc, beforeExecute, afterExecute);
  </code></pre>
  
  <p>However, I don't have an implementation of the required <code>wrap</code> function. Is there anything that already exists in jQuery like this (I've had a good look through the docs but cannot see anything)? Alternatively does anybody know of a good implementation of this because I suspect that there are a bunch of edge cases that I'll miss if I try to write it myself?</p>
  
  <p>(BTW - the reason for this is to do some automatic instrumentation of functions because we do a lot of work on closed devices where Javascript profilers etc. are not available. If there's a better way than this then I'd appreciate answers along those lines too.)</p>