class event {
construct(){
this.event = {};
}
function once(eventName, cb) = {
this.on(eventName,(...args) => {
cb(...args);
this.off(eventName);
});
}
function emit(eventName, ...args) {
if(this.event.hasOwnPropety(eventName)){
for(let cb of this.event[eventName]){
cb(...args);
}
}
}
function on(eventName, cb) {
if (!this.event.hasOwnPropety(eventName)){
this.event[eventName] = [];
}
this.event[eventName].push(cb);
}
function off(eventName) {
delete this.event[eventName];
}
}

本文深入探讨了事件驱动编程模式,介绍了如何通过构造事件监听器和触发器来创建响应式应用。具体包括定义事件监听器、绑定单次事件、触发事件、绑定事件以及解除事件绑定等关键操作。

被折叠的 条评论
为什么被折叠?



