ederollora
ederollora

Reputation: 1181

Can't understand nodejs code

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?

Upvotes: 0

Views: 86

Answers (1)

Amadan
Amadan

Reputation: 198314

As Kay says, this has a decent chance to be the object that triggered the event; but that is up to whoever wrote the code for Connection. If it does not, it might be giving the handler an argument that has the receiver. (You are not giving any details as to where Connection comes from, so we can't check for you what exactly it does and doesn't provide.) If all else fails, and the library designer failed to provide the receiving object anywhere, you can create a closure:

sshObjt.on('data',
  (function(receiver) {
    return function() {
      // CODE where `receiver` is correct
    };
  })(sshObjt)
);

As to the worry from your comment to Kay, that is unfounded. When you attach a handler to an object, you attach the handler to the object - not to the variable that contains it. It's just like this:

var annas_husband = { name: "Tim", occupation: "mechanic" }
var joeys_dad = annas_husband;
joeys_dad.occupation = "supervisor";
annas_husband // => { name: "Tim", occupation: "supervisor" }

You didn't change joeys_dad's occupation. You changed Tim's. Since both annas_husband and joeys_dad are Tim, it happens to both. And conversely, which pertains to your case:

var guy;
var guys = [];

guy = { name: "Jake", occupation: "mechanic" }
guys.push(guy);

guy = { name: "Tim", occupation: "mechanic" }
guys.push(guy);
guy.occupation = "supervisor"

guy = { name: "Jeff", occupation: "mechanic" }
guys.push(guy);

guys
// => [
//      { name: "Jake", occupation: "mechanic" },
//      { name: "Tim", occupation: "supervisor" }
//      { name: "Jeff", occupation: "mechanic" },
//    ]

Here, again, you didn't change guy's occupation, you changed Tim's. Jake and Jeff weren't the guy at that time, so they did not get the promotion. on works exactly the same way - it attaches an event to an object, regardless in which variable the object resides currently, or in the future.

Upvotes: 5

Related Questions