I’d like to have something like this work:
var Events=require('events'),
test=new Events.EventEmitter,
scope={
prop:true
};
test.on('event',function() {
console.log(this.prop===true);//would log true
});
test.emit.call(scope,'event');
But, unfortunately, the listener doesn’t even get called. Is there any way to do this w/ EventEmitter? I could Function.bind to the listener, but, I’m really hoping EventEmitter has some special (or obvious 😉 way to do this…
Thanks for the help!
No, because the
thisvalue in the listener is the event emitter object.However what you can do is this
The reason your event handler did not get called is because all the handlers are stored in
._eventsso if you copy._eventsover it should work.