觀察者模式

<script>
a=[]
Object.prototype.Push=function(){
	length=this.length
	setInterval(()=>{
		if(this.length > length ){
			this.register.Push.map(e=>e())
			length=this.length		
		}
	},0)
	return this
}	
Object.prototype.Pop=function(callback){
	length=this.length
	setInterval(()=>{
		if(this.length < length ){
			this.register.Pop.map(e=>e())
			length=this.length
		}
	},0)
	return this
}	
Object.prototype.on=function(event,callback){
	this[event](callback)	
}
a.register=[]
a.register.Push=[]	
a.register.Pop=[]
a.register.Push.push(()=>console.log(a),()=>console.log('Reactor模式'))
a.register.Pop.push(()=>console.log(a),()=>console.log('Reactor模式'))
a.Push().Pop()
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章