我如何包装发射器。on(eventName)?

问题描述 投票:0回答:1
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
  console.log('an event occurred!');
});
myEmitter.emit('event');

我该如何更改

myEmitter.on('event', () => {
   console.log('an event occurred!');
});

类似

onEvent(() => console.log('an event occurred!'))

我想对myEmitter.on('event')进行包装,所以我的onEvent本质上只是调用myEmitter.on('event')。这甚至可能吗?

javascript node.js
1个回答
0
投票

喜欢这个:

const onEvent = (callback) => {
  myEmitter.on('event', callback);
};

推荐问答