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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:59:43+00:00 2026-05-30T13:59:43+00:00

Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }

  • 0

Possible Duplicate:
Javascript infamous Loop problem?

I have the following:

function test(a,b,c){
    console.log(a+b+c);
}
for (var i=0; i < array.length; i++){
    steps.push(
        function(){ test(array[i].something, array[i].wow, i);}
    );

I want to store functions with several parameters, and bind them to buttons later when the DOM is loaded

for (var i=0; i<steps.length; i++){
    $('#mydiv li a').eq(i).click(steps[i]);
}

It’s not really working since steps[i] contains do(array[i].something, array[i].wow, i); instead of do(‘bob’, ‘john’, 1) for example

I have tried using the .apply javascript function but it will execute in the loop, instead of storing them

  • 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-30T13:59:45+00:00Added an answer on May 30, 2026 at 1:59 pm

    Closures can help you here. Here’s one solution to the first bit of code:

    function do(a,b,c){
        console.log(a+b+c);
    }
    for (var i=0; i < array.length; i++){
        (function (i) {
            steps.push(
                function(){ do(array[i].something, array[i].wow, i);}
            );
        })(i);
    }
    

    There’s no “block scope” in Javascript, just function scope. With your code, each function you pushed into the steps array had a reference to the last value of i, not the one it had when the function was pushed into steps. Invoking an immediately executing function enables the pushed function to have a value for i that doesn’t change, because a closure is being created. And this closure has a value for i that is independent of the loop’s value for i, which will be whatever is the last index of array.

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

Sidebar

Related Questions

Possible Duplicate: Javascript - array.contains(obj) What's wrong with this: var zipCodes =(['90001','90002','90003']); Test if
Possible Duplicate: Javascript infamous Loop problem? With the for loop, it does nothing. Without
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: Javascript: How to loop through ALL DOM elements on a page? var
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} Suppose we
Possible Duplicate: Javascript: var functionName = function() {} vs function functionName() {} Way 1:
Possible Duplicate: Explain JavaScript's encapsulated anonymous function syntax I have just read a javascript
Possible Duplicate: In Javascript, why is the “this” operator inconsistent? I have the following
Possible Duplicate: Javascript: var functionName = function() {} vs function functionName() {} Is there

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.