欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

手动实现js原生订阅发布事件模型

程序员文章站 2022-06-09 17:03:21
...
class Eventer {
	constructor() {
		this._events = {}
	}
	on(eName,fn) {
		if(eName in this._events) {
			this._events[eName].push(fn) //向已存在的事件中添加回调
		}else{
			this._events[eName] = [fn]
		}
	}
	
	emit() {
		let eName = [].shift.call(arguments) //获取事件名
		let args = [].slice.apply(arguments) //获取参数
		let self = this
		if(eName in this._events && this._events[eName].length>0) {
			this._events[eName].forEach(calback => {
				calback.apply(self,args)
			})
		}else {
			return false
		}
	}
	
	remove(eName, fn) {
		if(typeof eName !== 'string' || typeof fn !== 'function') {
			throw new Error(` ${eName} must be event name, ${fn} must be function type`)
		}
		if(eName in this._events) {
			this._events[eName] = this._events[eName].filter(callback => {
				return callback !== fn
			})
		}else{
			return false
		}
	}
	
	clear(eName) { //清楚所有监听事件
		if(typeof eName !== 'string') {
			throw new Error(`${eName} must be a string`)
		}
		if(eName in this._events) {
			this._events[eName] = []
		}
	}
	
	once(eName,fn) { //监听一次
		let self = this
		function wrap() {
			fn.apply(self,arguments)
			self.remove(eName,wrap)
		}
		if(eName in this._events) {
			this._events[eName].push(wrap) 
		}else{
			this._events[eName] = [wrap]
		}
	}
}