编辑代码

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) {
            // 执行type下的所有handle
            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')