Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:36:35+00:00 2026-05-26T12:36:35+00:00

I am supposed to have understood Cloures properly in Javascript, but I obviously didn’t…

  • 0

I am supposed to have understood Cloures properly in Javascript, but I obviously didn’t…

At the moment, the text I am reading has this function to abstract an AJAX call:

function request(url, callback){
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = (function(myxhr){
    return function(){
      callback(myxhr);
    }
  })(xhr);
  xhr.open('GET', url, true);
  xhr.send('');
}

Here is my actual problem: my brain is refusing to understand why this one wouldn’t work:

function request(url, callback){
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = callback(xhr);
  xhr.open('GET', url, true);
  xhr.send('');
}

I mean, in “my” way, what I imagine would happen is that I call say request(‘http://…’, a_callback). Within request(), a new xhr object is created, and it’s assigned to the callback… would wouldn’t it work? What would the (nasty) side effects be? From my (limited) understanding, you need closures when for example in a cycle you might end up referring to the latest value of a function variable. But here… doesn’t that “var xhr=…” make sure that a new object is created every time?

Please explain as if I had an IQ of 30 (which is probably true 😀 )

Merc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:36:35+00:00Added an answer on May 26, 2026 at 12:36 pm

    You don’t need that extra closure in the first example. This’d work fine:

    function request(url, callback){
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function(){
          callback(xhr);
      };
      xhr.open('GET', url, true);
      xhr.send('');
    }
    

    In modern browsers (or browsers patched with a polyfill) you could also do this:

    function request(url, callback){
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = callback.bind(null, xhr);
      xhr.open('GET', url, true);
      xhr.send('');
    }
    

    edit — also take note of the answer @Raynos provided. You don’t really need to pass the XHR object as a parameter.

    edit again — in response to a legit comment, you asked why your original idea wouldn’t work:

    function request(url, callback){
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = callback(xhr);
      xhr.open('GET', url, true);
      xhr.send('');
    }
    

    The problem is with the line here:

      xhr.onreadystatechange = callback(xhr);
    

    That assigns to the onreadystatechange the value returned from a call to the callback function right then and there. In other words, instead of establishing something to be called when the state changes, the call is made immediately. That error is really common, because it’s easy to read it incorrectly. Any time, however, JavaScript sees a function reference followed by a parenthesized argument list, that’s interpreted as a request to perform the function call, and not a request for a function wrapper around a future function call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposed I have the following string: string str = <tag>text</tag>; And I would like
I thought I understood JavaScript closures but apparently I don't. Take the following excerpt
I have a webservice method getContactsAsync. I've understood that when it's completed it's supposed
I have a bunch (hundreds) of files that are supposed to have Unix line
It seems that everybody knows you're supposed to have a clear distinction between the
In an N-Tier app you're supposed to have a business logic layer and a
I have a Serializable object which is supposed to hold a java.awt.Image as its
I have a page that is supposed to launch the Print Preview page onload.
I have an asp:ImageButton that is supposed to popup a save dialog when clicked.
I have a SQL query that is supposed to pull out a record and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.