title: How to dynamically change the contents of a function using JavaScript
body: <p>To help understand this the function is in the html page and it is generated, I cannot change the generated code:</p>
  
  <pre><code>function Update_qu7260() {
    var newVal = ''
    for( var idx = 0; idx &lt; 2; idx++ )
    {
      var test
      if( idx == 0 ) test = text7263
      else if( idx == 1 ) test = text7265
      if( test.matchObj ) newVal += test.leftSel + "-" + test.matchObj.rightSel + ","
    }
    newVal = newVal.substring( 0, newVal.length-1 )
    VarQuestion_0001.set( newVal )
    qu7260.hasBeenProcessed=false;
    doImmFeedback('qu7260');
  }
  var qu7260 = new Object();
  ...
  qu7260.updFunc = Update_qu7260;
  var qObj=[qu7260];
  </code></pre>
  
  <p>Note in the above the number "7260", the numbers start at 1 so there are lots of them and each <code>Update_###()</code> will be different so I cannot re-write them with "hard wired" code. My code is in an external JavaScript file and is executed onLoad:</p>
  
  <pre><code>...
  var updFunc = qObj[0].updFunc.toString();
  if(updFunc.indexOf('doImmFeedback(')!=-1){
    updFunc = updFunc.replace('doImmFeedback','doImmQuestionFeedback');  // do my function
    updFunc = updFunc.replace('function ','');  // remove the word function
    var funcName = updFunc.substr(0,updFunc.indexOf('('));  // get the function name e.g. Update_qu7260
    updFunc = "window['" + funcName + "']=function" + updFunc.replace(funcName,'');
    eval(updFunc);
  }
  ...
  </code></pre>
  
  <p>When I change the eval() to alert() I can see the that it's correct, however, the eval() is not raising any errors and my function <code>doImmQuestionFeedback</code> is not being called.  When I subsequently do an <code>alert(qObj[0].updFunc.toString())</code> I see the original function.</p>
  
  <p><strong>It would seem that I have provided information that is too complex, so the following code is a better example:</strong></p>
  
  <pre><code>function hi(){alert('hi');}
  function changeHi(){
     hi(); // I get an alert box with hi
     newHi = "function hi(){alert('hi there');}"
     eval(newHi);
     hi(); // I get an alert box with hi
     window.setTimeout('hi()',500); // I get an alert box with hi
  }
  window.setTimeout('changeHi()',500);
  </code></pre>
  
  <p><strong>The following is the original question:</strong></p>
  
  <p>I have a predefined function that I did not create, however, I know it's name so I can get the function itself and then I change it by doing:</p>
  
  <pre><code>var funcText = window.updateFunc.toString();
  funcText = funcText.replace('doSomeOtherFunction(','doMyFunction(');
  </code></pre>
  
  <p>How do I update the actual function so it will do all that it did before except it will now call doMyFuntion()?</p>
  
  <p>The following is an example to help visualize what I want to do, the actual function I need to change is very complex.  I have:</p>
  
  <pre><code>function updateFunc(whatToUpdate,true){
     ... - do lots of stuff.
     var retVal = doSomeOtherFunction(whatToUdate);
     ... - do lots of stuff based on retVal
  }
  </code></pre>
  
  <p>I need to change this to:</p>
  
  <pre><code>function updateFunc(whatToUpdate,true){
     ... - do lots of stuff
     var retVal = doMyFunction(whatToUdate);
     ... - do lots of stuff based on retVal, I have had a chance to change retVal
  }
  </code></pre>
  
  <p>Then the first thing my function will do is call doSomeOtherFunction() check/change the returned value and subsequently return the value to the updateFunc().</p>
  
  <p>I have tried to manipulate the funcText above to:</p>
  
  <pre><code>funcText = 'window.updateFunc = function(...';
  eval(funcText);
  </code></pre>
  
  <p>Without success.</p>