I know that Nodejs is supposed to be different to other laguages due to the event loop, what makes it difficult to understand at first. However there's no way I can understand the following code (I managed to make it work, but no logic explanation).
I create new instances of a SSH connection each time I receive an special request, let's say:
sshCons = {};
sshObjt = new Connection();
sshCons[request_id] = sshObjt;
sshObjt.connect(); //and son on...
So I create a new Connection and "store" it in what we can understand as an "associative array", even if I think that it's just an Object that carries properties and their values. The question is, if I place this code after the lines written above:
sshObjt.on('data', function() {
// CODE
});
How does Node know which sshObjt is the one that emits the event if I created multiple sshObjts and "stored" them? I previously tried to loop over the sshCons object but that shouldn't be the appropriate way of handling this. In case I'm wrong, which should be the proper way of handling events of multiple objects "stored" in sshCons object?
thiswill refer to the emitting "sshObjt".sshObjts" constantly and therefore "overwrite" the last created sshObjt, I thought Node would only attend the data event of the latest createdsshObjt. I hope I could explain myselfsshObjt, you are assigning it with the event handler fordataevent at the moment you create it. The current instance ofsshObjtis assigned with it's own even handler. It is more of a javascript question in general than node. :)