[英]Pass parameter to EventEmitter.on method in node.js
I have just started exploring node.js and below is situation with me while learning events handling in node.js. 我刚刚开始探索node.js,下面是我在node.js中学习事件处理的情况。
I have an event 'loop' and a function 'myLoopHandler' attached to it using the method 我有一个事件'loop'和一个函数'myLoopHandler'使用该方法附加到它
eventEmitter.on('loop',myLoopHandler);
And the myLoopHandler is defined as follows: myLoopHandler的定义如下:
var myLoopHandler = function(){
for(i=1;i<=30;i++)
console.log(i);
}
And then I emit the event 'loop' : 然后我发出事件'循环':
eventEmitter.emit('loop');
How do I pass some parameter to the myLoopHandler function in eventEmitter.on method? 如何在eventEmitter.on方法中将一些参数传递给myLoopHandler函数?
I am open to any other way of achieving the same. 我对任何其他实现同样目标的方式持开放态度。
just do 做就是了
emitter.emit(eventName[, ...args])
where args is the arguments to emit 其中args是发出的参数
this is an example 这是一个例子
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
console.log(a, b, this);
// Prints:
// a b MyEmitter {
// domain: null,
// _events: { event: [Function] },
// _eventsCount: 1,
// _maxListeners: undefined }
});
myEmitter.emit('event', 'a', 'b');
source NodeJS docs 源NodeJS文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.