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)

On 4/21/14, 3:11 PM, 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@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+unsubscr...@googlegroups.com
> <mailto:nodejs+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

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

-- 
-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to