柯里化

// 也称部分求职,先把参数存储到闭包内,到最后执行菜
var currying = function(fn) {
  var args = Array.prototype.slice(arguments, 1)
  return function() {
    var innerArgs = Array.prototype.slice(arguments);
    var finalArgs = args.concat(innerArgs);
    return fn.apply(null, finalArgs)
  }
}

柯里化-bind

// bind函数也是柯里化的一种,多了传入了上下文参数
var bind = function(fn, context) {
  var args = Array.prototype.slice(arguments, 2)
  return function() {
    var innerArgs = Array.prototype.slice(arguments);
    var finalArgs = args.concat(innerArgs);
    return fn.apply(context, finalArgs)
  }
}

反柯里化

Functions.prototype.uncurrying = function() {
	let self = this;
	return Function.prototype.call.apply(self, arguments)
}

函数节流

var throttle = function(fn, interval) {
	var _self = fn, timer, firstTime = true;
	return function() {
		var args = arguments,
			_me = this;
		if (firstTime) {
			_self.apply(_me, args);
			return firstTime = false;
		}
		if (timer) {
			return false;
		}
		
		timer = setTimeout(() => {
			clearTimeout(timer);
			timer = null;
			_self.apply(_me, args);
		}, interval || 500)
	}
}

window.onresize = throttle(function() {
	console.log(1);
}, 500)

分时函数

var timeChunk = function(arr, fn, count) {
	var obj, t;
	var len = arr.length;
	var start = function() {
		for (var i = 0; i < Math.min(count || 1, arr.length); i++) {
			var obj = arr.shift();
			fn(obj);
		}
	}
	
	return function() {
		t = setInterval(() => {
			if (arr.length === 0) clearInterval(t)
			start();
		}, 200);
	}
}

const test = new Array(1000).fill(1);
console.log(test);

const render = timeChunk(test, function(n) {
	let div = document.createElement('div');
	div.innerHTML = n;
	document.body.appendChild(div);
}, 8)
        render()
Last Updated:
Contributors: Nine