title: XMLHttpRequest asynchronous read, how not to use anonymous function?
body: <p>After searching Google for examples, all the examples I found use anonymous functions, like below. But I would like to avoid it because it makes the code complicated.</p>
  
  <pre><code>var xhr = new XMLHttpRequest(),
      method = "GET",
      url = "https://developer.mozilla.org/";
  
  xhr.open(method, url, true);
  xhr.onreadystatechange = function () {
    if(xhr.readyState === XMLHttpRequest.DONE &amp;&amp; xhr.status === 200) {
      console.log(xhr.responseText);
    }
  };
  xhr.send();
  </code></pre>
  
  <p>If I want to do like this, how can I pass the request or response?</p>
  
  <pre><code>function startRequest()
  {
      var xhr = new XMLHttpRequest(),
          method = "GET",
          url = "https://developer.mozilla.org/";
  
      xhr.open(method, url, true);
      xhr.onreadystatechange = myhandler;
      xhr.send();
  }
  
  function myhandler()
  {
    //how to get xhr here?
    if(xhr.readyState === XMLHttpRequest.DONE &amp;&amp; xhr.status === 200)
    {
      console.log(xhr.responseText);
    }
  }
  </code></pre>
  
  <p>I am more familiar with statically typed languages, so JavaScript is confusing. I tried to look the signature (input parameters) of onreadystatechange, but the documentation pages did not mention it. It seems that the documentations for dynamically typed languages (Python, PHP) tend to not describe the input parameters formally, so that I would have to guess what types of parameters the methods take.</p>
  
  <p>It has no parameters, and should I use global variable to pass the request and response to <code>myhandler</code>? Is there a better way?</p>