我试图“桥接”两个事件侦听器,以便用单个事件触发它们。而且,在这个“链”中,我可以将额外的值作为参数传递给后面的监听器。
我试图通过以下方式实现它
var events = require('events');
var receiv = new events.EventEmitter();
var midl = new events.EventEmitter();
receiv.on("test", function() {
console.log("HERE", arguments);
});
midl.on("test", receiv.on.bind(receiv, "test", "foo", "bar"));
midl.emit("test", 123);
然而,这种方法似乎对EventEmitter造成了错误,因为我收到了以下错误
TypeError: listener must be a function
at TypeError (<anonymous>)
at EventEmitter.addListener (events.js:130:11)
at EventEmitter.emit (events.js:95:17)
at Object.<anonymous> (myEventTest.js:12:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
干杯。
P.S。我使用节点v0.10.5
答案 0 :(得分:2)
对不起自我回答,但我找到了解决方案。
var events = require('events');
var receiv = new events.EventEmitter();
var midl = new events.EventEmitter();
receiv.on("test", function() {
console.log("HERE", arguments);
});
midl.on("test", receiv.emit.bind(receiv, "test", "foo", "bar"));
midl.emit("test", 123);