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

  • SEARCH
  • Home
  • 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 4034222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:56:42+00:00 2026-05-20T11:56:42+00:00

so I’m understanding closures and circular references (I hope), but one aspect is the

  • 0

so I’m understanding closures and circular references (I hope), but one aspect is the much reported closure within a loop which seems to cause much ado. I need clarification on it. The code I’m looking at is:

function showHelp(help) {
  document.getElementById('help').innerHTML = help;
}

function getHelp(help) {
  alert(help); 
  // only to show value of "help" at this point, also to prove that "getHelp()" is being called even
  // though the onfocus wasn't used

  return function() {
    showHelp(help);
  }
}

function setupHelp(){
  var helpText = [
    {'id':"email",'help':"Your email address"},
    {'id':"name",'help':"Your full name"},
    {'id':"age",'help':"Your real age"}
  ];

  for(var i=0;i<helpText.length;i++){
    var item = helpText[i];
    alert(item.help);
    // only to show value of "help" at this point, also to prove that "getHelp()" is
    // being called even though the onfocus wasn't used
    document.getElementById(item.id).onfocus = getHelp(item.help);
  }
}

The premise here being that you have three input fields and focusing them will return a helpful hint (as seen on the MDC article). But, here’s the crux of my lack of grasp: If you put an alert (so as to test) in the “getHelp()” function, and put one in the “setupHelp()” function, just before you set bind the event handler to a reference to the dom element, you’ll see that, on loading the page, the loop runs, then the getHelp() function, then the loop, then the getHelp() function, etc, until the end of the loop. So if the getHelp() function is bound to the onfocus event handler, and you don’t even focus the input fields, howe can the getHelp() function run?? And how does JAvaScript store all possible outcomes from that one little loop? :S

Any help you could provide would really help, this is mind boggling right now. I’m sure it’ll just click one of these days, but I’m impatient. 😛

Tom.

  • 1 1 Answer
  • 2 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-20T11:56:43+00:00Added an answer on May 20, 2026 at 11:56 am

    So if the getHelp() function is bound to the onfocus event handler, and you don’t even focus the input fields, howe can the getHelp() function run?? And how does JAvaScript store all possible outcomes from that one little loop?

    This is the crux of your question, and the answer lies here:

    for(var i=0;i<helpText.length;i++){
        var item = helpText[i];
        alert(item.help);
        // only to show value of "help" at this point, also to prove that "getHelp()" is
        // being called even though the onfocus wasn't used
        document.getElementById(item.id).onfocus = getHelp(item.help);
    }
    

    That’s how the JavaScript interpreter stores all of the possible outcomes: Because you told it what they were. You’re calling getHelp, which is generating a function, and then returning that function.

    How this works is much simpler than it seems. 🙂 I go into it a fair bit here, but basically: When you call a function, something called an execution context is created. That’s an object (JavaScript is massively object-oriented, right down to the interpreter level). In that execution context object, there’s something called the variable object. It holds all of the variables for the execution context, as properties. This includes the arguments to the function, all the vars within the function, and any declared functions as well (you don’t have any declared functions in your example, so we can ignore that; you have only function expressions, which is fine). Any function declared or defined by expression within an execution context has an enduring reference to the variable object for that execution context, and uses it to resolve variable references when it’s called.

    So: In your loop, when you call getHelp, an object is created storing the data related to that call. That object is bound to the function that you’re creating within the call and storing on the onfocus handler (that’s the data that the function closes over [which is why it’s called a closure]). When/if the handler is called, that’s how the references that function holds are resolved, against properties on that object.

    More reading: Closures are not complicated

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.