Question about `this`

87 views
Skip to first unread message

Jazear Brooks

unread,
Sep 4, 2014, 11:27:26 PM9/4/14
to nod...@googlegroups.com
Consider the following function assignment:

var EventEmitter = require("events").EventEmitter;

EventEmitter.prototype.once = function(type, callback) { var that = this;

   
this.on(type, function listener() {
        that
.removeListener(type, listener);
        callback
.apply(that, arguments);


}); };

Also consider the following two variables:

var a = new EventEmitter
var b = new EventEmitter.prototype


Does the `this` on line 5 of the first code section refer to variables a, b, or both?  Is variable b even declared legally?

greelgorke

unread,
Sep 5, 2014, 4:22:36 AM9/5/14
to nod...@googlegroups.com
'this' the same as 'a'. Your declaration of b is weird lead to an error, because EventEmitter.prototype is an object.

Jazear Brooks

unread,
Sep 5, 2014, 12:54:24 PM9/5/14
to nod...@googlegroups.com
Great, thanks
Reply all
Reply to author
Forward
0 new messages