麻烦帮忙看下这个写的有什么可以优化的地方吗?
目前我比较在意的两个问题:
class AppEvent {
constructor() {
this.EventType = {
Type1: "Type1",
Type2: "Type2",
};
let self = this;
self.app_events = {};
self.emit = function (eventName, data) {
let callbackArr = self.app_events[eventName];
if (callbackArr && callbackArr.length) {
for (let i = 0; i < callbackArr.length; i++) {
if (callbackArr[i]) {
callbackArr[i](data);
}
}
}
};
self.on = function (eventName, callback) {
if (!self.app_events[eventName]) {
self.app_events[eventName] = []
}
self.app_events[eventName].push(callback)
};
self.off = function (eventName, callback) {
let callbackArr = self.app_events[eventName];
if (callbackArr && callbackArr.length) {
let index = callbackArr.indexOf(callback);
callbackArr.splice(index, 1);
}
}
}
}