我将一个事件监听器存储在WeakMap中,并在检索该函数以将其从EventEmitter中删除时,会抛出此错误:
TypeError:listener必须是函数
我无法弄清问题是什么:
var EventEmitter = require('events').EventEmitter;
var listeners = new WeakMap();
var eventEmitterA = new EventEmitter();
var eventEmitterB = new EventEmitter();
eventEmitterA.on('open', (obj) => {
function stuff() { /* ... */ }
listeners.set(obj, stuff);
eventEmitterB.on('stuff', stuff);
});
eventEmitterA.on('close', (obj) => {
var listener = listeners.get(obj);
console.log(typeof listener); // logs "function"
eventEmitterB.removeListener(listener);
});
var someObject = {some: 'object'};
eventEmitterA.emit('open', someObject);
eventEmitterA.emit('close', someObject);