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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:57:36+00:00 2026-06-04T07:57:36+00:00

In the following snippet, why and how does the calling the function form a

  • 0

In the following snippet, why and how does the calling the function form a brand new execution context where the value of i is retained?

function getHandler(n) {
    return function() {
        alert( 'You clicked on: ' + n );
    };
}
 
for (var i = 0; i < 100; ++i) {
    myElements[i].onclick = getHandler(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-06-04T07:57:36+00:00Added an answer on June 4, 2026 at 7:57 am

    It’s caused by JavaScript closures and the behavior of variables declared in a for loop in JavaScript. Because the argument n is part of addHandler‘s closure, n maintains it’s value for the function instances declared inside of itself. It just so happens that your passing in i from a for loop in the global space.

    Here is a fiddle showing that behavior.

    If you were to do something to increment n inside of addHandler, you would see that it doesn’t actually effect i. Once again, this is because of closure, n exists inside of addHandler‘s closure and was merely populated by i.

    Here is a fiddle showing that behavior.

    Because of closure, n will exist for however long whatever is created inside of addHandler (in this case some function references) exist.

    I hope that makes sense. It’s tricky to explain, I think.

    EDIT: Here is an awesome explanation of JavaScript closures and how they work.

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

Sidebar

Related Questions

The following Python snippet does exactly what I mean: def function(a, b, c): print(%i
Given the following snippet: function countZeroes(array) { function counter(total, element) { return total +
I have the following snippet of code: $('input#teamName').live('blur', function() { var value = $(this).val();
I have the following snippet of java code: File directoryToMoveTo = new File(file.getParent()+_TEMP); boolean
What I would like to ask you is, why the following snippet does not
What does isset($_SESSION) mean? I found the following code snippet- if (!isset($_SESSION)) { //
Can someone please tell me what the following snippet obj.send("#{method_name}") is and does?
I wonder why the following snippet does not give the expected result? It sorts
What does the phrase std::string::npos mean in the following snippet of code? found =
does the following snippet throw NPE when argument is null? public void doSomething(String string)

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.