偏函数
Sunny-117 opened this issue · comments
偏函数
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)