javascript 具有不同参数的 eventEmitter 侦听器和发射器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24414842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:45:13  来源:igfitidea点击:

eventEmitter listeners and emitters with different parameters

javascriptnode.jsmochaprotractoreventemitter

提问by nirvanastack

Can we have multiple listeners of an emitter, each working on different number of arguments?

我们可以有一个发射器的多个监听器,每个监听器处理不同数量的参数吗?

e.g. let event emitter be like this:

例如让事件发射器是这样的:

evetE.emit('pre', global, file, self);
corresponding event listeners:
//Listener 1

m.eventE.on('pre', function() {
//TODO
})

//Listener 2
eventE.on('pre', function(context, file, m){
  console.log(context.ans);
});

//Listener 3
eventE.on('pre', function(context){
  console.log(context.ans);
});

//Listener 4
this.eventE.on('pre',function (context) {})

If the above is true, then which parameter goes to which listener?

如果以上是真的,那么哪个参数去哪个监听器?

回答by mynameisdaniil

Event listeners just regular JS functions. So you can pass as many arguments as you want, but function only can access those arguments you have declared in function definition i.e.

事件监听器只是普通的 JS 函数。因此,您可以根据需要传递任意数量的参数,但函数只能访问您在函数定义中声明的那些参数,即

var EE = require('events').EventEmitter;
var ee = new EE();

ee.on('test', function (first, second, third) {
  console.log(first, second, third); //Will output full phrase
});

ee.on('test', function (first) {
  console.log(first); //Will output just first word
});

ee.on('test', function () {
  console.log.apply(console, arguments); //Will output full phrase again
});

ee.emit('test', 'Hello', 'my', 'world!');

Actually you can see that all provided arguments always passed to every function. But if you don't define argument names in function declaration you won't be able to directly access this arguments. But you can use magic "arguments" object inside every function to access all provided arguments. Ofcourse, argument provided to the function in the order they passed to EE.

实际上你可以看到所有提供的参数总是传递给每个函数。但是如果你没有在函数声明中定义参数名称,你将无法直接访问这个参数。但是您可以在每个函数中使用魔法“参数”对象来访问所有提供的参数。当然,参数按照传递给 EE 的顺序提供给函数。

回答by Hayes

The EventEmitter appears to call all listeners using the applymethod. Therefore, every listener can expect to receive arguments in the same order passed to the emitfunction. The following code demonstrates that the parameterless listener still receives all arguments to the function.

EventEmitter 似乎使用apply方法调用所有侦听器。因此,每个侦听器都可以期望以传递给emit函数的相同顺序接收参数。以下代码演示无参数侦听器仍接收函数的所有参数。

var EventEmitter = require('events').EventEmitter;

var ee = new EventEmitter();

ee.on('myEvent', function() {
    console.log('no arguments');
    console.log(arguments); // Outputs: { '0': 'arg 1', '1': 'arg 2' }
});

ee.on('myEvent', function(arg1, arg2) {
    console.log('with arguments');
    console.log(arg1);
    console.log(arg2);
});

ee.emit('myEvent', 'arg 1', 'arg 2');