Created
August 16, 2013 19:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: the call back is run in the context of Test() yay.