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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:28+00:00 2026-05-20T23:15:28+00:00

Sample 1: http://jsfiddle.net/ufCr8/ function createFunctions() { var result = new Array(); for (var i

  • 0

Sample 1:http://jsfiddle.net/ufCr8/

function createFunctions() {
    var result = new Array();

    for (var i = 0; i < 10; i++) {
        result[i] = function() {
            return i;
        }();
    }

    return result;
}

var funcs = createFunctions();       
for (var i = 0; i < funcs.length; i++) {
    document.write(funcs[i] + "<br />");
}

Sample 2:http://jsfiddle.net/T5shB/

function createFunctions() {
    var result = new Array();

    for (var i = 0; i < 10; i++) {
        result[i] = function(num) {
            return function() {
                return num;
            };
        }(i);
    }

    return result;
}

var funcs = createFunctions();
for (var i = 0; i < funcs.length; i++) {
    document.write(funcs[i]() + "<br />");
}

Why do those two get different results?

  • 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-20T23:15:29+00:00Added an answer on May 20, 2026 at 11:15 pm

    These both work correctly. (And have the same results).

    Sample #1:

    for (var i = 0; i < 10; i++) {
        result[i] = function() {
            return i;
        }(); // note function application!
    }
    

    The anonymous function is executed right then and the result is a number (the current) value of i each iteration through the loop. This is effectively the same as result[i] = i. Not very exciting. (In the loop that prints the value out, there is no function application, which would be an error as a number if not a function — in this case it differs from the samples below.)

    Sample #2:

    for (var i = 0; i < 10; i++) {
        result[i] = function(num) {
            return function() {
                return num;
            };
        }(i);
    }
    

    The outer anonymous function which is applied returns a closure which correctly “double-binds” to the current value of i which is passed in as num (num is actually the bound free-variable). Remember that functions introduce a new scope — var (or for) does not.

    I suspect the “failing case” would be:

    for (var i = 0; i < 10; i++) {
        result[i] = function() {
            return i;
        }; // note function NOT invoked here!
    }
    
    ...
    for (var i = 0; i < funcs.length; i++) {
        document.write(funcs[i]() + "<br />");
    }
    

    This will produce “odd results” because it is the same i which is bound in each closure (that is i is the free variable and there is only one i). Thus when the function is executed it will return the current value of i at the point of execution.

    Happy coding


    I would recommend reading the Jibbering JavaScript Closure Notes — it’s not the most beginner resource, but I find it accessible and it explains things in sufficient detail. (And is far, far more readable than the ECMAScript specification, IMOHO).

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

Sidebar

Related Questions

I am working on a sample problem on jsFiddle at http://jsfiddle.net/Q9yHD/ I am trying
I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
All of this is code is here: http://jsfiddle.net/yrgK8/ I have a news section which
Having some issues with my dropdown menu. Code here: http://jsfiddle.net/xY2p6/1/ Something simple I'm just
I have a base URL : http://my.server.com/folder/directory/sample And a relative one : ../../other/path How
I am serializing a class using simple-xml ( http://simple.sourceforge.net/ ) but when i try
I have a simple jQuery function: $('#selectable1 span').live('mousedown', function() { var ff = $(this).css(font-family);
I need to periodically download, extract and save the contents of http://data.dot.state.mn.us/dds/det_sample.xml.gz to disk.
There is a simple http server API that allow you do some simple stuff
I want to create a simple http proxy server that does some very basic

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.