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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:09:33+00:00 2026-06-13T04:09:33+00:00

Assume socketMethods:{ update:function(data){ this.emit(test, data); console.log(socket.update); }, test:function(data){ this.emit(test, data); console.log(socket.test); } } This

  • 0

Assume

socketMethods:{
    update:function(data){
        this.emit("test", data);
        console.log("socket.update");
    },
    test:function(data){
        this.emit("test", data);
        console.log("socket.test");
    }
}

This below is possible, and allows me to emit “test” and “update”, and “test” and “update” will be called

socket.on("test", function(data){
    dummy.socketMethods.test.call(socket, data);
});
socket.on("update", function(data){
    dummy.socketMethods.update.call(socket, data);
});

These below are NOT possible… EACH one will instead call “test”, if I emit “test” OR “update”. If I use a FOR loop, all methods will become the “last” method called.

That is: If I switched update and test so that test is first, and update is second in the socketMethods object. “update” would always be called, regardless of whether I asked for “test” or “update”

for(key in dummy.socketMethods){
    socket.on(key, function(data){
        dummy.socketMethods[key].call(socket, data);
    });
}



var methods = ["test", "update"];
while(methods.length !== 0){
    var method = methods.pop();
    console.log(method);
    socket.on(method, function(data){
        dummy.socketMethods[method].call(socket, data);
    });
}



var methods = ["test", "update"];
for(var i = 0; i < methods.length; i++){
    console.log(i);
    socket.on(methods[i], function(data){
        console.log(i);
        dummy.socketMethods[methods[i]].call(socket, data);
    });
}

CLUE : The for loop using ‘i’, has two console.logs. The second console.log returns “2”

Why does this happen? And how could I use a for loop to attach my socketMethods?

  • 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-13T04:09:34+00:00Added an answer on June 13, 2026 at 4:09 am

    You’re making the common mistake of assuming that a closure “traps” the value of an outer function’s variable at the time it was created. It doesn’t. It “traps” a reference to the outer function’s variable, and its value will be whatever the outer variable has at the time the closure is executed. If you’re creating closures in a loop, that value will be whatever it had at the end of the loop.

    The way to get around this is to create an “enclosing” closure that uses the outer variable’s value as argument. One way to do this is to create a helper function that generates your closure. In your first example, you could do

    function makeOn(key) {
      return function(data) {
        dummy.socketMethods[key].call(socket, data);
      }
    }
    
    for(key in dummy.socketMethods){
      socket.on(key, makeOn(key));
    }
    

    Now in each closure, key will be a newly-created variable (differing between closures) set to the appropriate value.

    It’s also possible to use an immediate function call (what some call a “self-executing function”) to do the same thing a little more succinctly, but also somewhat less readably.

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

Sidebar

Related Questions

Assume I've this parser: let test p str = match run p str with
Assume I want to unit test a method with this signature: List<MyItem> getMyItems(); Assume
Assume this situation: interface IFoo { public function Bar(); } trait Foo { public
assume you have a unit test that contains these lines assertNotNull(someVal); assertNotEmpty(someVal); This obviously
Assume I have two tables, Student Test Id Name TestId Type StudentId -- ----
Assume I have the following ; def test(): while 1: a = b time.sleep(60)
Assume this code works: left '*' left '+' expr: expr '+' expr | expr
Assume you have a function read_key and normally it does some stuff. You someone
Assume I've declared a function (or class, doesn't matter) in a header file, which
assume this is file.txt Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final Annette Adams :8:24:19:24:10:12:43:35:98 Mary Beard:9:30:19:23:10:14:29:39:87 Antoinette Brown:9:16:18:22:9:12:19:31:79 . .

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.