手写普通函数转化为柯里化函数


function whCurrying(fn) {
	return function curried(...args1) {
		if (args1.length >= fn.length) {
			return fn.apply(this, args1);
		} else {
			return function curried2(...args2) {
				// return curried.apply(this, [...args1, ...args2]);
				return curried.apply(this, args1.concat(args2));
			};
		}
	};
}

function sum(num1, num2, num3) {
	return num1 + num2 + num3;
}

let fn = whCurrying(sum);

console.log(fn(1)(2)(3)); //6

相关