title: setTimeout() inside JavaScript Class using "this"
body: <p>I am trying to use <code>setTimeout()</code> inside a class function in JavaScript. The <code>setTimeout()</code> is supposed to trigger another method in the same Class, so the function I am passing it is written as <code>window.setTimeout("this.anotherMethod", 4000)</code>. That bring the problem: <code>this</code> references the calling Object, in the case of <code>setTimeout()</code> it is <code>window</code>. How can I use enclosures to return a reference to the Class Object itself?</p>
  
  <pre><code>myObject = function(){
  
  this.move = function(){
      alert(this + " is running");
  }
  this.turn = function(){
      alert(this + " is turning");
  }
  this.wait = function(){
      window.setTimeout("this.run" ,(1000 * randomNumber(1,5)));
  }
  
  this.run = function(){
      switch(randomNumber(0,2)){
          case 0:
              this.move();
          break;
          case 1:
              this.turn();
          break;
          case 2:
              this.wait();
      }
  }
  </code></pre>
  
  <p>}</p>