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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:22:27+00:00 2026-06-16T01:22:27+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example I am trying to

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example

I am trying to do something like this:

var obj = function(){
    var members = ['a','b','c','d','e'];
    for(var i in members){
       inst[i] = function(){
           console.log(i);
       }
    }
};

But when I do:

var inst = new obj();
inst.a(); inst.b(); inst.c(); inst.d(); inst.e();

It prints out e 5 times! Also, I would likely to be able to reference this rather than using the name that I am instantiated the object as, but I cannot get that to work at all. The logic behind this is to use websockets to generate an API template client side. This is a rough demo that does basically the same type of thing (logically) but I am getting caught up in this technical issue and it is keeping me from progressing.

Thanks

EDIT

Let me give the complete issue. I didn’t want to overload everyone but these solutions aren’t working…

var SocketClient = function(config){
//platform specific stuff
var self = this;
var count = 0;
var socket = io.connect(config.address+':'+config.port);
var members = [];
socket.on('method',function(data){
    console.log(data.name);
    KovarApp.client[data.name] = addLogicMember(data.name);
    console.log(KovarApp.client[data.name]);
});
var addLogicMember = function(n){
    return function(config){
        var callback = null;
        if(typeof config.callback !== 'undefined'){
            var callback = config.callback;
            delete config.callback;
        }
        call({
            method: n,
            data: config,
            callback: callback
        });
    };
};
var call = function(config){
    var c = count++;
    var timer = null;
    socket.on('reply_'+c,function(data){
        if(typeof config.callback === 'function'){
            if(timer != null){
                timer.clearTimeout();
            }
            config.callback(data);
            timer = setTimeout(function(){
                socket.removeAllListeners('reply_'+c)
            },10000);
        }
    });
    config.data.handle = c; //make sure that the server has the handle for this function call
    socket.emit(config.method,config.data);
};

};

The server emits 5 functions to the client: login being the first function and getpatientinfo being the last function. On the server, I have it set up to console.log every reply. If I call any of the functions, it console.log’s getpatientinfo because that is the string that it receives, meaning that on the client every single function is referencing the last received string (getpatientinfo). I have changed it according to some of the answers, but it still isn’t working. Socket shouldn’t be an issue here. The issue is obviously my front-end logic when generating members, as every member reflects the last added member.

Solved! See below! (THIS WASN’T CLOSURE!!!)

  • 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-16T01:22:29+00:00Added an answer on June 16, 2026 at 1:22 am

    This is called ‘a closure effect’: each of your functions knows that it should deal with variable named i, but will take its current value when invoked.

    To solve this, scope i as well:

    var makeMeAFunction = function(x) {
      return function() { console.log(x); };
    };
    for(var i in members){
       inst[i] = makeMeAFunction(i);
    }
    

    As a sidenote, it’s usually a bad idea iterating over an Array with for...in: use forEach or, if you just have to support IE8, resort to traditional for(init; check; step) operator.

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

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example I was trying to
Possible Duplicate: Javascript closure inside loops - simple practical example I'm trying to use
Possible Duplicate: Javascript closure inside loops - simple practical example Please explain the use
Possible Duplicate: Javascript closure inside loops - simple practical example javascript variable scope/closure in
Possible Duplicate: Javascript closure inside loops - simple practical example I have an array
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: Javascript closure inside loops - simple practical example I'm creating a table
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice
Possible Duplicate: Javascript closure inside loops - simple practical example How can I make

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.