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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:50:38+00:00 2026-06-06T03:50:38+00:00

I am reading about closures in Javascript: The Good Parts book. There is following

  • 0

I am reading about closures in “Javascript: The Good Parts” book.

There is following example of using closures:

var add_the_handlers = function (nodes) {
var i;
   for (i = 0; i < nodes.length; i += 1) {
      nodes[i].onclick = function (i) {
         return function (e) {
            alert(i + ":" + e);
         };
      }(i);
   }
};

Is it correct example? Or much correct example would be following?

var add_the_handlers = function (nodes) {
var i;
   for (i = 0; i < nodes.length; i += 1) {
      nodes[i].onclick = function (idx) {
         return function (e) {
            alert(idx + ":" + e);
         };
      }(i);
   }
};

Variable i in the outer function and variable i in the inner function “nodes[i].onclick = function (i)” – it is two different variables.
And third function accesses variable from second function, not from the outermost.
Am I correct?

  • 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-06-06T03:50:39+00:00Added an answer on June 6, 2026 at 3:50 am

    The two examples are identical. The whole point of a closure is to make an outer-scoped variable (i) into an inner-scoped variable (i/idx/foo, take your pick). The closure creates a “copy” of the variable, so that when the callback gets made, it has the correct value.

    // outer-scoped i changes on each iteration
    var i;
    
    for (i = 0; i < nodes.length; i += 1) {
       nodes[i].onclick = function (i) {
    
          // here i now refers to a different variable; while the outer i keeps iterating,  
          //this i is preserved at its current value.
          return function (e) {
             alert(i + ":" + e);
          };
       }(i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while reading about declare construct from php manual i tried the following example using
I've been reading some JavaScript books and I always hear about closures and side
I was reading some posts about closures and saw this everywhere, but there is
I have been reading about closures and javascript, and I thought I got it,
while reading about cookies i found difficulty to grasp the following statement setcookie() must
When reading about SQL I've come across the term 'slot'. For example, in a
Reading about and using the Amazon Web Services, I'm not really able to grasp
Reading about both separatedly, looks like the same, html+xml+javascript. What's the difference between then?
When reading about JQuery best practices, I read this recently: Never include Javascript events
After reading about the pimpl idiom I was horrified! Isn't there a tool out

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.