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 thethis._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
1 Answer
Reset to default 16Shouldn'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
版权声明:本文标题:javascript - apply() does not work on Node.js eventemitter's emit() function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741493954a2381748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论