function myEvent() {
let handleObj = {}
return {
on: function(type, handle) {
if(handleObj[type]) {
handleObj[type].push(handle)
}else {
handleObj[type] = [handle]
}
},
off: function(type, handle) {
if(handleObj[type]) {
handleObj[type] = handleObj[type].filter(item => item !== handle)
}
console.log(handleObj[type])
},
once: function(type, handle) {
this.on(type, handle)
handle()
this.off(type, handle)
},
empty: function(type) {
handleObj[type].forEach(item => {
item()
})
}
}
}
let a = new myEvent()
let a1 = function() {
console.log(1)
}
let a2 = function() {
console.log(2)
}
let a3 = function() {
console.log(3)
}
let a4 = function() {
console.log(4)
}
let a5 = function() {
console.log(5)
}
a.on('click', a1)
a.on('click', a2)
a.on('click', a3)
a.on('click', a4)
a.empty('click')
a.off('click', a1)
a.off('click', a2)
a.once('click', a5)
a.empty('click')