1

Most of the Node API docs don't use this keyword inside event handler. For example below code prefers readable.read() instead of this.read(). Although both works fine.

Using "this" makes it generic and we can define callback function outside and can be used as reference inside on method.

Thoughts please.

API doc

Example1: why not this.read() ?

readable.on('readable', function() {
  var chunk;
  while (null !== (chunk = readable.read())) {
    console.log('got %d bytes of data', chunk.length);
  }
});

Example2: why not this.address() ?

server.listen(function() {
  address = server.address();
  console.log("opened server on %j", address);
});

Note: Event section of API says "Inside a listener function, this refers to the EventEmitter that the listener was attached to. " but I don't see said practice at all in the example codes.

P K
  • 8,662
  • 12
  • 48
  • 90
  • 1
    http://stackoverflow.com/questions/3127429/javascript-this-keyword – mak Apr 06 '14 at 10:59
  • I'm guessing in some cases the object can contain more intances / a collection, and `this` would only reference the current one etc. – adeneo Apr 06 '14 at 10:59
  • adeneo, Sorry didn't get – P K Apr 06 '14 at 11:02
  • When chaining like this, especially with event handlers, `this` doesn't neccessarely reference the entire object, but only the "part" that triggered the event, there's no guarantee that `this` inside the callback is exactly the same as the object chained to the property/method. – adeneo Apr 06 '14 at 11:12
  • You're definitely right on the key point: inside event handlers, `this` refers to EventEmitter. It follows the same convention as other Event implementations, and it does make sense, actually. ) But there's another key point: all these examples are from API _documentation_. That's why they should be as clean as possible - and using `this` hardly makes things easier to understand. – raina77ow Apr 06 '14 at 11:15
  • Thanks for clarification, in browser side event handler uses "this" very frequently but in case of node, API doc as well all popular books uses the approach that I mentioned above. – P K Apr 06 '14 at 11:17
  • @PK Not always, have to say: for example, check [EventEmitter2](https://github.com/asyncly/EventEmitter2) implementation: its examples actually never use the second approach. ) And `popular books` have the same reason not to use `this` in their examples - clarity. – raina77ow Apr 06 '14 at 11:19
  • Ok makes sense, somehow I prefer this over the actual variable name :) – P K Apr 06 '14 at 11:21
  • As long as you know what `this` is, and that it is in fact what you want, that's fine, but in some cases you don't want just that instance, but all of them. socket.io is a good example of using event handlers that reference a socket in `this`, while the `sockets` object the events are bound to reference all current sockets etc. and it still passes the current socket as an argument to be used instead of `this`. `this` also bound to the execution context, and it's not very reliable for every concieveable use. I use `this` everywhere in Node, and it's not an issue as long as you know what it is – adeneo Apr 06 '14 at 11:39
  • @adeneo Didn't know about sockets and socket part of socket.io, I will check that. But in case if we have just one object, "this" should be fine I guess. Thanks – P K Apr 06 '14 at 11:45

0 Answers0