admin管理员组

文章数量:1290191

The apply() method does not work on Node's eventemitter's emit function.

I execute these 2 statements:

this._baseEmitter.emit('activity', {test: 'zever1'});

this._baseEmitter.emit.apply(this, ['activity', {test: 'zever2'}]);

The first 1 runs fine, and the event is captured by my listener.

The second one, however, does absolutely nothing.

Does anyone know why? Is the emit() function perhaps missing the apply method? If so, I would think I would receive some error messages, but that's not the case either.

The apply() method does not work on Node's eventemitter's emit function.

I execute these 2 statements:

this._baseEmitter.emit('activity', {test: 'zever1'});

this._baseEmitter.emit.apply(this, ['activity', {test: 'zever2'}]);

The first 1 runs fine, and the event is captured by my listener.

The second one, however, does absolutely nothing.

Does anyone know why? Is the emit() function perhaps missing the apply method? If so, I would think I would receive some error messages, but that's not the case either.

Share Improve this question asked Jan 31, 2013 at 0:13 Jelle De LoeckerJelle De Loecker 22k29 gold badges104 silver badges146 bronze badges 2
  • var emitApply = Function.apply.bind(this._baseEmitter.emit, this._baseEmitter) ...will give you a function that you can call like: emitApply(['activity', {test: 'zever2'}]); This assumes the this._baseEmitter context doesn't change. If it does, remove it as the last argument to .bind(), and provide it for each call. – the system Commented Jan 31, 2013 at 0:24
  • 3 You should accept @PascalBelloncle's answer, as it worked for you. – Dan Phillimore Commented Feb 3, 2013 at 16:14
Add a ment  | 

1 Answer 1

Reset to default 16

Shouldn't the first parameter be this._baseEmitter instead?

this._baseEmitter.emit.apply(this._baseEmitter, ['activity', {test: 'zever2'}]);

本文标签: javascriptapply() does not work on Nodejs eventemitter39s emit() functionStack Overflow