when to use self instead of this

95 views
Skip to first unread message

Reza Razavipour

unread,
Apr 21, 2014, 4:11:14 PM4/21/14
to nod...@googlegroups.com
I see a lot of code that does 
var self = this

and then they use self instead of this for the duration of the function?

Why and when would you use the above pattern?


Matt

unread,
Apr 21, 2014, 4:13:02 PM4/21/14
to nod...@googlegroups.com
It's good practice because in callbacks "this" is no longer what you thought it was, whereas that self variable will be. I tend to do it out of habit even if there are no callbacks because there might be one some day down the line.


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reza Razavipour

unread,
Apr 21, 2014, 4:26:58 PM4/21/14
to nod...@googlegroups.com
would you still do that if you want to change something about this? mix self and this?

Regards,
Reza


You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/3YvqPyTIt-0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

Aria Stewart

unread,
Apr 21, 2014, 4:53:54 PM4/21/14
to nod...@googlegroups.com
Consider this:

var obj = {
method: function() {
// Right here, this == obj.
setTimeout(function () {
// Right here, this is something else.
}, 1000);
}
};
obj.method();

If you do this:

var obj = {
method: function() {
// Right here, this == obj.
var self = this;
setTimeout(function () {
// Right here, this is something else.
// But self is still lexically scoped — it’s the same as it was.
}, 1000);
}
};
obj.method();

The rules in javascript say that ‘this’ is dynamically scoped — generally, it’s the object the method was called on, though you can use Function#call and Function#apply and Function#bind to change it. For non-method functions like callbacks, though, it’s something else. (Depending on strict mode and the details of the call).

Because of what it does (too much!), Function#bind is slow. So we manually enclose the ‘this’ in the parent scope to bind it lexically, rather than dynamically. Since you can’t declare a new ‘this’ variable, we use self out of convention — though that’s not universal. Any name will work.

Aria
signature.asc

Matt

unread,
Apr 21, 2014, 5:39:25 PM4/21/14
to nod...@googlegroups.com

On Mon, Apr 21, 2014 at 4:26 PM, Reza Razavipour <reza.ra...@gmail.com> wrote:
would you still do that if you want to change something about this? mix self and this?

I would never mix the two. Everything is a reference in Javascript so changes to self are changes to this.

Jose Luis Rivas

unread,
Apr 21, 2014, 7:05:52 PM4/21/14
to nod...@googlegroups.com
A.prototype.a (cb) {
// I do something here
cb(null, someData);
};

A.prototype.b () {
self = this;
// it could be `this.a` as well.
self.a(function (err, response) {
self.c(response); // will work
this.c(response); // wont work
});
};

A.prototype.c (data) {
console.log(data);
};

It's all about the scope. `this` is local to each function.

http://ejohn.org/apps/learn/ can be of great help understanding this
kind of things (and a lot more)
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to nodejs+un...@googlegroups.com
> <mailto:nodejs+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Jose Luis Rivas - http://joseluisrivas.net
Venezuela - GPG: 0xB9AC8C43

Alex Kocharin

unread,
Apr 21, 2014, 9:49:54 PM4/21/14
to nod...@googlegroups.com
 
 
22.04.2014, 00:11, "Reza Razavipour" <reza.ra...@gmail.com>:
When you are:
 
1. need to use callbacks
2. unable to use arrow functions
3. unwilling to explicitly bind each callback to `this`
 
There are people known to survive without using that pattern at all.
 

Andrey

unread,
Apr 22, 2014, 12:44:24 AM4/22/14
to nod...@googlegroups.com
In addition all previous comments:
I don't like name 'self' as alias to 'this' ( I tend to interpret same way as 'this', e.i context of current function). Instead, use same name you would use for your object instance. Example:

Client.prototype.doStuff = function() {
  var client = this;
  // ... some async code here
  setTimeout(function() {
     client.doneTimeout(); // I think it's more explicit than self.doneTimeout()
  }, 100);
};

George Stagas

unread,
Apr 22, 2014, 3:29:56 AM4/22/14
to nod...@googlegroups.com
2014-04-22 7:44 GMT+03:00 Andrey <andrey....@gmail.com>:
In addition all previous comments:
I don't like name 'self' as alias to 'this' ( I tend to interpret same way as 'this', e.i context of current function). Instead, use same name you would use for your object instance. Example:

Client.prototype.doStuff = function() {
  var client = this;
  // ... some async code here
  setTimeout(function() {
     client.doneTimeout(); // I think it's more explicit than self.doneTimeout()
  }, 100);
};
  


Consider the situation where that `client.something()` ends up somewhere deeply nested and the function isn't that trivial. Someone reading that piece code or jumping to it from search results will have no idea what it points to, because it's not obvious; is it an argument, or what? Where did it come from?

These questions waste time and energy. `self` is conventionally the instance object for that reason. Conventions are there for that reason.

Then there's refactoring, what if you decide it's not a Client but something else. Suddenly `client = this` doesn't make sense. What if you move the function over to another prototype because it seems it belongs better there? Again, doesn't make sense. Save yourself and everyone else some headaches and use `self`. It can never be wrong.
 

On Tuesday, 22 April 2014 06:11:14 UTC+10, Reza Razavipour wrote:
I see a lot of code that does 
var self = this

and then they use self instead of this for the duration of the function?

Why and when would you use the above pattern?


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

Glenn Block

unread,
Apr 22, 2014, 3:31:12 AM4/22/14
to nod...@googlegroups.com
Douglas Crockford recommends "that" as the alias for this which is an approach that I prefer over self. Self reminds me of Python :-)


George Stagas

unread,
Apr 22, 2014, 3:42:07 AM4/22/14
to nod...@googlegroups.com
2014-04-22 10:31 GMT+03:00 Glenn Block <glenn...@gmail.com>:
Douglas Crockford recommends "that" as the alias for this which is an approach that I prefer over self. Self reminds me of Python :-)


When I see `that` instead of `self` I think to myself: "oh, that person never reads any code" because if you've been reading `self` in 99% of the code out there you wouldn't be doing something else just because a random person of authority said so.

Andrey

unread,
Apr 22, 2014, 4:52:18 AM4/22/14
to nod...@googlegroups.com


On Tuesday, 22 April 2014 17:29:56 UTC+10, stagas wrote:



2014-04-22 7:44 GMT+03:00 Andrey <andrey....@gmail.com>:
In addition all previous comments:
I don't like name 'self' as alias to 'this' ( I tend to interpret same way as 'this', e.i context of current function). Instead, use same name you would use for your object instance. Example:

Client.prototype.doStuff = function() {
  var client = this;
  // ... some async code here
  setTimeout(function() {
     client.doneTimeout(); // I think it's more explicit than self.doneTimeout()
  }, 100);
};
  


Consider the situation where that `client.something()` ends up somewhere deeply nested and the function isn't that trivial. Someone reading that piece code or jumping to it from search results will have no idea what it points to, because it's not obvious; is it an argument, or what? Where did it come from?

That's exactly the reasoning behind my suggestion: 'self' or 'that' deeply nested from corresponding 'this' much more difficult to understand than properly named reference. Also it's more difficult to fine original reference in a file with multiple 'self' of different type scattered in different places
Reply all
Reply to author
Forward
0 new messages