gpt4 book ai didi

javascript - 如何包装emitter.on(eventName)?

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:12 25 4
gpt4 key购买 nike

const myEmitter = new MyEmitter();
myEmitter.on('event', (a) => {
console.log('an event occurred! ' + a);
});
myEmitter.emit('event', "a");

我该如何改变

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

类似于

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

我想在 myEmitter.on('event') 周围有一个包装器,所以我的 onEvent 本质上只是调用 myEmitter.on('event')。这可能吗?

最佳答案

const onEvent = cb => myEmitter.on('event', cb);

如果您想处理不同的事件,还可以执行以下操作:

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

关于javascript - 如何包装emitter.on(eventName)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com