javascript 監聽模式

(function(){
	var _push  = [].push,
	      _splice = [].splice;
	function Monitor1(argument) {
		this.sources = [];

		this.registerListener = function(key,val){
			var target = {};
			target.key = val;
			_push.call(this.sources,target);
		}

		this.removeListener = function(key){
			for(var i = 0; i = sources.length; i++){
				if(typeof this.sources[i][key])
					return _splice.call(this.sources,i,1);

			}
		}

		this.triggerListener = function(key){
			var monitorContext = this;
			for(var i = 0; i < this.sources.length; i++){

				if(typeof this.sources[i][key] === 'function' ){
					return this.sources[i][key].call(monitorContext,err);
				}
			}

		}
	}
var monitor = new Monitor1();

monitor.registerListener("testEvent",function(){

	alert("testEvent.....");
})

monitor.triggerListener("testEvent");
	
})();
相關文章
相關標籤/搜索