SOURCE

console 命令行工具 X clear

                    
>
console
function curry(fn, ...args) {
    if (typeof fn !== 'function') {
        throw 'no function provided'
    }
    if (args.length < fn.length) {
        return function (...params) {
            return curry(fn, ...args, ...params)
        }
    }
    return fn(...args)
}


// 示例1
function mysetTimeout(cb, delay) {
    setTimeout(cb, delay)
}
curry(mysetTimeout, () => console.log("hello1"), 1000)

let delayTenMsCurry = curry(mysetTimeout);
delayTenMsCurry(() => console.log("hello2"), 2000);

let delayTenMs = delayTenMsCurry(() => console.log("hello3"));
delayTenMs(3000)

// 示例2
function addFn(a, b, c, d, e, f, g) {
    return a + b + c + d + e + f + g
}
let add = curry(addFn);

// 全部参数一次性传递
console.log(add(1, 2, 3, 4, 5, 6, 7))

// 参数分批传递
console.log(add(1)(2, 3, 4)(5, 6, 7))

// 每个参数单独传递
console.log(add(1)(2)(3)(4)(5)(6)(7))
<div>
    <h2>函数柯里化</h2>
    <p>
        概念:对函数降维,将一个多参函数,经过函数链的形式,转化成接收一个参数的函数
    </p>
</div>
body{
    background: #333;
    color:white
}