0

I'm trying to do...

EventEmitter = require('events').EventEmitter

events = new EventEmitter()

events.emit.apply(null, ['eventname', 'arg1', 'arg2', 'arg3'])

...but it doesn't seem to work or throw an error, any help?

boom
  • 10,856
  • 9
  • 43
  • 64

2 Answers2

1

On Apply method you need inform two arguments: A valid scope (what value will be the "this" inside the method) and the argument array.

Everton Tavares
  • 78
  • 1
  • 1
  • 7
0

I think the emit() function of the EventEmitter is expecting this to be an actual EventEmitter object. The following seems to work if you pass in the events object.

events.emit.apply(events, ['eventname', 'arg1', 'arg2', 'arg3'])

Though at this point, might as well just call

emit('eventname', 'arg1', 'arg2', 'arg3')

I am guessing the emit() method is looping over an internal list of registered events to see if there is a matching one. Not sure if this helps, just something I came across today working with events...

cengebretson
  • 296
  • 3
  • 4