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?