Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

偏函数 #74

Open
Sunny-117 opened this issue Nov 3, 2022 · 2 comments
Open

偏函数 #74

Sunny-117 opened this issue Nov 3, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@veneno-o
Copy link
Contributor

function partial(fn, value){
    return function(...args){
        return fn(value,...args)
    }
}

@gswysy
Copy link

gswysy commented Mar 5, 2024

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants