title: Creating functions dynamically in JS
body: <p>I am creating the AI engine for a JS game, and it's made of Finite State Machines. I am loading the number of states and their variable values from the XML. I also want to load the behaviour, and since I don't have the time to create a scripting language, I thought it would be a good idea to 'insert' JS code on external files (inside XML nodes), and execute it on demand.</p>
  
  <p>Something like that</p>
  
  <pre><code>&lt;evilguy1&gt;
      &lt;behaviour&gt;
          this.x++;
      &lt;/behaviour&gt;
      &lt;behaviour&gt;
          this.y++;
      &lt;/behaviour&gt;
  &lt;/evilguy1&gt;
  </code></pre>
  
  <p>To something like that:</p>
  
  <pre><code>function behaviour_1(){
      this.x++;
  }
  function behaviour_2(){
      this.y++;
  }
  </code></pre>
  
  <p>My question is, now that I have the code loaded, how can I execute it? I would like to create a function with an unique name for each code 'node', and then call them from the game logic, but I don't know if this is possible (Since you can load more JS code from the HTML, you should also be able to do it from the JS code, no?). If not, is there any similar solution? Thanks in advance!</p>
  
  <p>(PS:The less external-library-dependent, the better)</p>
  
  <p>Edit 1:</p>
  
  <p>Ok, so now I know how to create functions to contain the code</p>
  
  <pre><code>window[classname] = function() { ... };
  </code></pre>