function Events() {
this.on = function() {
if (!this.handles) {
this.handles = {}
}
if (!this.handles[eventName]) {
this.handles[eventName] = []
}
this.handles[eventName].push(callBack)
}
this.emit = function(eventName, obj) {
if (this.handles[eventName]) {
for(var i = 0; i < this.handles[eventName].length; i++) {
this.handles[eventName][i](obj)
}
}
}
return this
}
// 调用实例
var events = new Events()
events.on('say', function(name) {
console.log('Hello', name)
})
events.emit('say', 'jony yu')
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。