手写eventhub
作者:
littleyu | 来源:发表于
2021-05-13 14:10 被阅读0次function EventHub () {
this.events = {}
}
EventHub.prototype.$on = function(name, fn) {
(this.events[name] || (this.events[name] = [])).push(fn)
}
EventHub.prototype.$emit = function (name, ...args) {
this.events[name] && this.events[name].map(dn => fn.call(undefined, ...args))
}
EventHub.prototype.$off = function (name, fn) {
const events = this.events[name] && this.events[name].filter(v => v !== fn)
if (events) {
if (events.length === 0) {
delete this.events[name]
} else {
this.events[name] = events
}
}
}
本文标题:手写eventhub
本文链接:https://www.haomeiwen.com/subject/oqmlwctx.html
网友评论