Skip to content

Instantly share code, notes, and snippets.

@mostlygeek
Created August 16, 2013 19:58
Show Gist options
  • Save mostlygeek/6253051 to your computer and use it in GitHub Desktop.
Save mostlygeek/6253051 to your computer and use it in GitHub Desktop.
A little test to see what context an event listener is bound to when run.
const events = require('events'),
util = require('util');
function Test() {
this.created = Date.now();
}
util.inherits(Test, events.EventEmitter);
Test.prototype.sendEmit = function() {
this.emit("BOOM", Date.now(), this);
}
var t = new Test();
t.on('BOOM', function(time, t2) {
console.log("I am in the context of t: ", (this == t));
});
setTimeout(function() {
t.sendEmit();
}, 100);
@mostlygeek
Copy link
Author

Results: the call back is run in the context of Test() yay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment