Javascript Question

1 view
Skip to first unread message

Randolph

unread,
Oct 7, 2010, 10:39:32 PM10/7/10
to Mongo node .js driver
Hi!

I have seen some parts of the code, where the author assigns the
"self" variable with "this" like and used them like on the code below:

// Add connnect listener
this.connection.addListener("connect", function() {
this.setEncoding("binary");
this.setTimeout(0);
this.setNoDelay();
self.emit("connect");
});

Is this and self referring to a different object in this case?

Kristina Chodorow

unread,
Oct 7, 2010, 10:57:45 PM10/7/10
to mongo-node...@googlegroups.com
No, self and this are the same.  I think he's using the two to distinguish between methods that belong to the connection class and those that it inherits from EventEmitter.  So, anything defined in Connection is called with this.whatever(), anything defined in EventEmitter is called with self.whatever().

Randolph

unread,
Oct 28, 2010, 10:20:56 PM10/28/10
to Mongo node .js driver
I just learned today that self and this does not refer to the same
thing! It appears that this always refers to the owner of the
function. self was referring to the owner of the outer function, which
was the Connection class of the driver, while this was referring to
the owner of the inner function, the net.stream class, which calls
addListener.

On Oct 7, 10:57 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> No, self and this are the same.  I think he's using the two to distinguish
> between methods that belong to the connection class and those that it
> inherits from EventEmitter.  So, anything defined in Connection is called
> with this.whatever(), anything defined in EventEmitter is called with
> self.whatever().
>

Kristina Chodorow

unread,
Oct 29, 2010, 1:54:31 PM10/29/10
to mongo-node...@googlegroups.com
Good to know!  Sorry for the misinformation.
Reply all
Reply to author
Forward
0 new messages