title: socket.io Using a for loop to add socket.on methods
body: <p>Assume</p>
  
  <pre><code>socketMethods:{
      update:function(data){
          this.emit("test", data);
          console.log("socket.update");
      },
      test:function(data){
          this.emit("test", data);
          console.log("socket.test");
      }
  }
  </code></pre>
  
  <p>This below is possible, and allows me to emit "test" and "update", and "test" and "update" will be called</p>
  
  <pre><code>socket.on("test", function(data){
      dummy.socketMethods.test.call(socket, data);
  });
  socket.on("update", function(data){
      dummy.socketMethods.update.call(socket, data);
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>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"</p>
  
  <pre><code>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 &lt; methods.length; i++){
      console.log(i);
      socket.on(methods[i], function(data){
          console.log(i);
          dummy.socketMethods[methods[i]].call(socket, data);
      });
  }
  </code></pre>
  
  <p>CLUE : The for loop using 'i', has two console.logs.  The second console.log returns "2"</p>
  
  <p>Why does this happen?  And how could I use a for loop to attach my socketMethods?</p>