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 6144245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:37:23+00:00 2026-05-23T18:37:23+00:00

The below example was taken from the book, Javascript: The good parts. The author

  • 0

The below example was taken from the book, “Javascript: The good parts”. The author says that the helper function returns a function that binds to the current value of var i.

Can anyone explain what makes it to bind the VALUE instead of REFERENCE of var i, because helper function is a closure to add_the_handler function and should only see the reference of var i:

var add_the_handlers = function (nodes) {
   var helper = function (i) {
      return function (e) {
        alert(i);
      };
    };
    var i;
    for (i = 0; i < nodes.length; i += 1) {
       nodes[i].onclick = helper(i);
    }
};
  • 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-23T18:37:23+00:00Added an answer on May 23, 2026 at 6:37 pm

    If you were to say:

    nodes[i].onclick = function(){ alert(i) };
    

    The function would not have it’s own copy of i because i is not declared within the scope of the function.

    To help you see this better I’ve modified your above code:

    var add_the_handlers = function (nodes) {
        var helper = function(t) {
          // t is in the scope of "helper"
          return function(e){
            // e is in the scope of this anonymous function
            // and is not used
            alert(t);
          };
        };
    
        // Variables declared here are in the scope of "add_the_handlers"
        var i;
        for (i = 0; i < nodes.length; i += 1) {
           nodes[i].onclick = helper(i);
        }
    };
    

    In the “real world” you’ll often see code like the above shortened to look like this:

    var add_the_handlers = function(nodes){
        var i;
        for(i = 0; i < nodes.length; i++)
           nodes[i].onclick = (function(i){ return function(e){ alert(i); }; })(i);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below example is taken from this documentation page: https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields [Example start] You can set
The code below is more or less taken from the example MPMoviePlayerController sample code.
I want to fetch a method's comments,take below method for example: /** * Returns
How can I get only unique departments from the below example? Dept Id Created
Currently i am creating an extension method that accepts parameters. Using the below example,
The example below, is just an example, I know that I don't need an
below content is taken from Best practice: Writing efficient code but i didn't understand
I have taken an example taught to us in class,wherein a javascript is used
Hi there's a live version of the code below (taken from a tutorial) at
Here is an example of text taken from database and displayed on the page:

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.