2014-04-22 7:44 GMT+03:00 Andrey <[email protected]>:

> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to