Function Prototype

思考??? 
 
var IDispatchable = {		
	map: function(handler) {
		return this.dispatch(function(args, callback) {
			callback.call(this, handler.apply(this, args));
		});
	},
	
	filter: function(handler) {
		return this.dispatch(function(args, callback) {
			if (handler.apply(this, Array.slice(args))) {
				callback.apply(this, args);
			}		
		});
	},
	
	throttle: function(delay) {
		var timer;
		return this.dispatch(function(args, callback) {
			var 				
				me = this
			;
			
			clearTimeout(timer);
			timer = setTimeout(function() {
				callback.apply(me, args);
			}, delay);
		});
	},
	
	delay: function(delay) {
		return this.dispatch(function(args, callback) {
			var 
				me = this
			;
						
			setTimeout(function() {
				callback.apply(me, args);
			}, delay);
		});
	},
		
	numb: function(delay) {
		var is_numb = false;
		return this.dispatch(function(args, callback) {
			if (!is_numb) {
				is_numb = true;
				callback.apply(this, args);
				setTimeout(function() {
					is_numb = false;
				}, delay);
			}
		});
	}
};




Function.prototype.dispatch = function(dispatcher) {
	var fn = this;	
	return function() {
		var callback = Array.pop(arguments);		
		fn.apply(this, Array.arrayPush(arguments, function() {
			dispatcher.call(this, arguments, callback);
		}));
	};
};




Function.proto(IDispatchable);


调用方式如:


……
_display: function(items) {
		var selected_items = this.selected_items;
		MultipleAutoComplete.superclass._display.call(this, filter(items, function(item) {
			return !find(selected_items.getData(), function(o) {
				return o.value == item.value
			});
		}));		
	},
……




你可能感兴趣的:(Function Prototype)