class EventBus {
constructor() {
this.events = this.events || new Object();
}
}
EventBus.prototype.emit = function (type, ...args) {
const eventFuncs = this.events[type];
if (Array.isArray(eventFuncs)) {
for (let index = 0; index < eventFuncs.length; index++) {
eventFuncs[index].apply(this, args)
}
}
else {
eventFuncs.apply(this, args)
}
};
EventBus.prototype.addListener = function (type, func) {
const eventFuncs = this.events[type];
if (!eventFuncs) {
this.events[type] = [func]
}
else {
eventFuncs.push(func)
}
}
EventBus.prototype.removeListener = function (type, func) {
if (this.events[type]) {
const eventFuncs = this.events[type]
if (Array.isArray(eventFuncs)) {
if (func) {
const funcIndex = eventFuncs.findIndex(eventFunc => func === eventFunc)
if (funcIndex !== -1) {
eventFuncs.splice(funcIndex, 1)
}
else {
console.warn(`eventBus may remove unexit func(${type})`)
}
}
else {
delete eventFuncs[type]
}
}
else {
delete eventFuncs[type]
}
}
}
const eventBus = new EventBus();
eventBus.addListener("test", function (...args) {
console.log(args);
});
eventBus.emit("test", [1, 3]);
var func = function() {
console.log('func')
}
var obj = {
f: func
}
var obj2 = {
f: func
}
console.log(obj.f === obj2.f)
var arr = [1, 3, 2]
arr.splice(1, 1)
console.log(arr)