Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

偏函数

Sunny-117 opened this issue · comments

commented
偏函数
function partial(fn, value){
    return function(...args){
        return fn(value,...args)
    }
}

偏函数:先将函数的一些参数固定化,后续只需要传递剩余参数即可,减少重复传参

function partial(fn, ...args) {
    if (typeof fn !== 'function') {
        throw new TypeError(`${typeof fn} is not a function`)
    }
    return function (...arr) {
        fn.apply(fn, args.concat(arr))
    }
}
function a(a, b, c) {
    console.log(a, b, c)
}

const fn = partial(a, 1, 2)

fn(3)