发布订阅小示例

it2022-05-09  28

const Event = { eventSkinks: {},  //添加监听事件  //监听事件,支持自定义事件,如果事件不存在,创建该事件数组,并将回调放入事件池。 on (type, cb) { if (this.eventSkins.hasOwnProperty(type)) { this.eventSkins[type].push(cb); } else { this.eventSkins[type] = []; this.eventSkins[type].push(cb); } return this; },//触发事件,如果该事件存在,遍历事件回调,执行所有的回调函数。 trigger (type, data, thisArg) { if (this.eventSkins[type]) { this.eventSkinks[type].forEach(function(func) { func.call(thisArg || this, [type, data]); }.bind(this)); } return this; }}// 创建对象时继承Event事件基类 const Widget = Object.create(Event);

转载于:https://www.cnblogs.com/fancyLee/p/8030254.html


最新回复(0)