【代碼筆記】js高階函數的應用

/*
*  判斷變量類型 
*  Type.isXXX( obj );
*/
var Type = {};
/* @type 註冊類型 */
for( var i = 0, type; type = [ 'Array', 'String', 'Number' ][ i++ ]; ){
	( function  ( type ) {
		Type[ 'is' + type ] = function  ( obj ) {
			return Object.prototype.toString.call( obj ) === "[object " + type + "]";
		}
	})( type )
}

/*
*  AOP實現 
*  func()
*/
/* @beforefn 原函數執行以前的處理函數 */
Function.prototype.before = function  ( beforefn ) {
	var _self = this;
	return function  () {
		beforefn.apply( this, arguments );
		return _self.apply( this, arguments );
	}
}
/* @afterfn 原函數執行以後的處理函數 */
Function.prototype.after = function  ( afterfn ) {
	var _self = this;
	return function  () {
		var ret = _self.apply( this, arguments );
		afterfn.apply( this, arguments );
		return ret; 
	}
}
var func = function  () {
	console.log(2);
}
/* 包裝 */ 
func = func.before(function  () {
	console.log(1);
}).after(function  () {
	console.log(3);
})

/*
*  curring 函數柯里化
*/
/* @fn 傳參長度爲0時執行fn,不然將參數保存起來 */
var currying = function  ( fn ) {
	var args = [];
	return function  () {
		if ( arguments.length === 0	){
			return fn.apply( this, args );
		}else{
			[].push.apply( args, arguments );
			return arguments.callee;
		}
	}
};
var cost = ( function  () {
	var money = 0;
	return function  () {
		for (var i = 0, l = arguments.length ; i < l; i++ ) {
			money += arguments[i];
		}
		return money;
	}
})();
/*
* uncurring 
*/
//第一種
Function.prototype.uncurrying = function  () {
	var _self = this;
	return function  () {
		var obj = Array.prototype.shift.call( arguments );
		return _self.apply( obj, argument );
	}
}
//第二種
Function.prototype.uncurring = function  () {
	var self = this;
	return function  () {
		return Function.prototype.call.apply( self, argument );
	}
}

/*
*  函數節流:延遲執行一些高頻率調用的函數,如window.onresize。限制函數被頻繁調用。
*/
var throttle = function  ( fn, interval ) {
	var _self = fn,
	    timer,
	    isFirstTime = true;
	return function  () {
		var args = arguments,
		    _me = this;
		    if( isFirstTime ){ //第一次調用不須要延遲
		    	_self.apply( _me, args );
		    	return isFirstTime = false;
		    }
		    if ( timer ){ //計時器還在,表示上一次的延遲執行尚未完成
		    	return false;
		    }
		    timer = setTimeout(function  () {
		    	clearTimeout( timer );
		    	timer = null;
		    	_self.apply( _me, args );
		    }, interval || 500);
	}
}

/*
*  分時函數:分批執行函數
*/
/* $ary 創造節點時須要的參數 */
/* $fn 創造節點的邏輯 */
/* $count 每一批建立的節點數量 */
var timeChunk = function  ( ary, fn, count ) {
	var obj,
		t;
	var start = function  () {
		for( var i = 0; i < Math.min( count || 1, ary.length ); i++){
			var obj = ary.shift();
			fn( obj );
			console.log(ary.length);
		}
	};
	return function  () {
		t = setInterval(function  () {
			if ( ary.length == 0){
				return clearInterval( t );
			}
			start();
		}, 200)
	}
}

/*
*  惰性加載函數:減小重複卻沒必要要的方法耗時 addEvent示例
*/
var addEvent = function  ( elem, type, handler ) {
	if( window.addEventListener ){
		addEvent = function  ( elem, type, handler ) {
			elem.addEventListener( type, handler, false );
		}
	}else if ( window.attachEvent ){
		addEvent = function  ( elem, type, handler ) {
			elem.attachEvent( 'on' + type, handler );
		}
	}
	addEvent( elem, type, handler );
}
相關文章
相關標籤/搜索