class EventEmitter {
constructor() {
this.events = {};
}
on(type, listener, isUnshift) {
if (!this.events) {
this.events = {};
}
if (this.events[type]) {
if (isUnshift) {
this.events[type].unshift(listener);
} else {
this.events[type].push(listener);
}
} else {
this.events[type] = [listener];
}
if (type !== "newListener") {
this.emit("newListener", type);
}
}
emit(type, ...args) {
if (this.events[type]) {
this.events[type].forEach((fn) => fn.call(this, ...args));
}
}
once(type, listener) {
const me = this;
function oneTime(...args) {
listener.call(this, ...args);
me.off(type, oneTime);
}
me.on(type, oneTime);
}
off(type, listener) {
if (this.events[type]) {
const index = this.events[type].indexOf(listener);
this.events[type].splice(index, 1);
}
}
}
let event = new EventEmitter();
event.on("say", function (str) {
console.log(str);
});
event.once("say", function (str) {
console.log("这是once:" + str);
});
event.emit("say", "visa");
event.emit("say", "visa222");
event.emit("say", "visa333");