7

I know the "self" magic. But look at this snippet from nodejs(not complete).

Socket.prototype.connect = function(options, cb) {
......
  var self = this;
  var pipe = !!options.path;

  if (this.destroyed || !this._handle) {
    this._handle = pipe ? createPipe() : createTCP();
    initSocketHandle(this);
  }

  if (typeof cb === 'function') {
    self.once('connect', cb);
  }

  timers.active(this);

  self._connecting = true;
  self.writable = true;
......
}

It is my understanding that we must use self to create a closure. Here there is no closures in these lines but the author use both after assigning this to self. Does it make any difference here?

2
  • I assume it's used only because of convention and unity with the rest of the code. Using this in one place might just be an oversight.
    – JJJ
    Commented Apr 18, 2013 at 5:18
  • 1
    since self is set to this they're the same variable. I'm not sure why the author mixes the two. Commented Apr 18, 2013 at 5:24

1 Answer 1

4

In what you've shown in this particular code example, there is no reason to even have the self variable because there are no other function scopes that might need access to the original value of this.

Some developers have a consistent methodology or convention to create a local variable like self and assign it the value of this just so that they have it to use, if needed, in closures. The self variable can also be minimized smaller than this because it can be renamed to a one character variable name, but this cannot be renamed.

In any case, the functionality here would not be affected if self was removed and only this was used in this particular method.

My own personal convention is to only define self if it is actually needed which is the same logic I use for other local variables and then I only use it inside the closure where it is needed.

4
  • The code is not complete. There are closures in this function. I just didn't post it. So the creation of self makes sense.
    – Alvin Cao
    Commented Apr 18, 2013 at 5:39
  • @AlvinCao - if there are anonymous callback functions defined inside this method and those callback functions want access to the original value of this, then yes this is a common design pattern to assign the value of this to a local variable which is often called self or that or me so that that local variable can be used inside the callback function to refer to the host object when this has a different value in the callback function.
    – jfriend00
    Commented Apr 18, 2013 at 5:42
  • Thank you, @jfriend00. But I'm afraid I'm not asking the convention of using a self variable. I know why. I'm asking in such non-closure code why the author use both words. I doubt if the author is complying to any convention to make the code clearer because I see code like this many times.
    – Alvin Cao
    Commented Apr 18, 2013 at 5:54
  • @AlvinCao - it is inconsistent and generally a bad practice to use this in part of the method and self in other parts of the method when self is not needed. If you're going to use self, then use it everywhere or only when required in a closure, not sometimes but not others. That's inconsistent and makes the code confusing and makes readers of the code wonder what's going on (when there is nothing going on).
    – jfriend00
    Commented Apr 18, 2013 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.