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 this
value in the listener is the event emitter object.
However what you can do is this
var scope = {
...
};
scope._events = test._events;
test.emit.call(scope, ...);
The reason your event handler did not get called is because all the handlers are stored in ._events
so if you copy ._events
over it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With