自定义事件原生实现
2020-05-02 本文已影响0人
海豚先生的博客
类似jquery中的事件绑定和触发
let Event = {}
Event.on = function (type,cb) {
this.handler = this.handler || {}
this.handler[type] = this.handler[type] || []
this.handler[type].push(cb)
console.log(this);
}
Event.trigger = function (type) {
this.handler[type].forEach(cb => {
cb()
});
}
Event.on('click', function () {
console.log('click1');
})
Event.on('click', function () {
console.log('click2');
})
Event.trigger('click')