title: Javascript, function inside function problem
body: <p>Hey, I was working with a javascript project and reached a problem that I just don't understand. Here is the code, it's not the one that I use in my project, but it's a simplified version.</p>
  
  <pre><code>var x;
  
  function FetchBox() {alert("Worked");}
  
  function A(m,n) { 
      x = new XMLHttpRequest(); 
      x.open("GET", m, true);
      x.onreadystatechange=function(){
          n(); 
          x.send();
      };
  }
  
  A("http://jsfiddle/echo/xml/", FetchBox);
  </code></pre>
  
  <p>I can easily change the function to make it work:</p>
  
  <pre><code>function A(m,n) { 
      x = new XMLHttpRequest(); 
      x.open("GET", m, true);
      x.onreadystatechange=n();x.send();
  }
  </code></pre>
  
  <p>But in my more complex version I want to add a readyState function and a few other things.</p>
  
  <pre><code>function A(m,n) { 
      x = new XMLHttpRequest(); 
      x.open("GET", m, true);
      x.onreadystatechange=
          if(x.readyState===4){
              n(); 
              x.send();
          };
  }
  </code></pre>
  
  <p>Why can't I include a function inside this function? JsFiddle link: <a href="http://jsfiddle.net/M6Upv/17/" rel="nofollow">http://jsfiddle.net/M6Upv/17/</a></p>
  
  <p>Have a good weekend,
  Ulrik</p>