Can you intercept a local variable in a closure without eval?
<p>I am writing some JavaScript profiling bits and need to be able to intercept methods inside a closure. </p>

<p>I managed to get this working: </p>

<pre><code>var t = (function() {

  var echo = function(v) { console.log("calling echo with " + v); }; 

  return {
    intercept: function(n, f) {
      var old = eval(n);
      var newFunction = (function(that, old){
        return f(that, old);
      })(this, old);
      eval(n + " = newFunction ");
    },
    getEchoFunction: function() { return echo; }
  };  
})(); 


var c = t.getEchoFunction();

c("hello");



t.intercept("echo", function(that,old){
  return function() {
    console.log("before echo");
    old.apply(that,arguments);
    console.log("after echo");
  };
});


c = t.getEchoFunction();

c("world");
</code></pre>

<p>Output is:</p>

<pre>
"calling echo with hello"
"before echo"
"calling echo with world"
"after echo"
</pre>

<p>So, this "intercept" API lets me intercept and re-write function declarations hidden in a closure. </p>

<p>However, there is much complaining about eval in the world. </p>

<p>Is there any way to write the same API without needing to use <code>eval</code> in the <code>intercept</code> function? </p>
