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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:34:14+00:00 2026-06-02T02:34:14+00:00

I have just been writing some tests when a fundamental problem emerged. I wrote

  • 0

I have just been writing some tests when a fundamental problem emerged. I wrote the bare skeleton of a test framework. It first collects all tests (their callbacks) in an array and executes them afterwards in a separate loop. However, this

//test.js
var testArray = [
    {
    n : 1,
    d : 'text1'
    },
    {
    n : 2,
    d : 'text2'
    }
];

var cbs = [];
function fnWithCallback(d, cb) {
    console.log('d=('+d+')');
    cbs.push(cb);
}

for(var i=0; i < testArray.length; i++) {
    var v = testArray[i];

    fnWithCallback(v.d, function() {
    console.log('v=('+v.n+'), i=('+i+')');
    });
}

for(var j=0; j < cbs.length; j++) {
    cbs[j]();
}

When I run this sample I get this:

> node test.js
d=(text1)
d=(text2)
v=(2), i=(2)
v=(2), i=(2)

That means in the callback ‘v’ has been assigned to the last element of the last array and ‘i’ to it’s last state, not to the one when the callback has been ‘created’ and passed to the ‘fnWithCallback’ function. However, since ‘d’ is printed out within the callback it hast the value of the element when ‘fnWithCallback’ has been called.

“Unlooping” the loop does not help.

var v = testArray[0];

fnWithCallback(v.d, function() {
    console.log('v=('+v.n+')');
});

v = testArray[1];

fnWithCallback(v.d, function() {
    console.log('v=('+v.n+')');
});

results in the same behaviour.

Can someone explain this and provide a solution?

  • 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-02T02:34:16+00:00Added an answer on June 2, 2026 at 2:34 am

    Use a closure

    fnWithCallback(v.d, (function(v,i){
        return function() {
            console.log('v=('+v.n+'), i=('+i+')');
        };
    })(v,i)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just been involved in writing an application that does not use Doctype's
I have just been helped on a problem I have here . var win
I have been writing some code that detects add and removal of USB devices,
I have just been looking into Linq with ASP.Net. It is very neat indeed.
Okay, I have just been through a massive amount of bull craparooonie with xcodes
I'm currently using the hilo id generator for my classes but have just been
i have a SQL server database on a server. I have just recently been
I am a complete Noob when it comes to GIT. I have been just
I have just started C very recently and I have been asked to answer
I've been programming for many years and have just recently got the RoR itch.

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.