js 函数柯里化和防抖

function forcall(fun) {
  console.log(fun)
  return function f1(...args) {
    console.log(11, args)
    if (args.length >= fun.length) {
      return fun(...args)
    } else {
      return (...moreargs) => {
        console.log(22, moreargs)
        const newargs = args.concat(moreargs)//[...args, ...moreargs]
        return f1(...newargs)
      }
    }
  }
}
function add(a, b, c) {
  return a + b + c;
}
const aa = forcall(add)
const ret = aa(2)(4)(3)
console.log(ret)

 

function debounce(fun, duration = 500) {
  let timerId;
  return function (...args) {
    clearTimeout(timerId)
    timerId = setTimeout(() => {
      fun.apply(this, args)
    }, duration);
  }
}
function add(a, b) {
  console.log(a + b)
}
const fun1 = debounce(add, 3000)
fun1(2, 3)

 

posted @ 2024-04-26 11:05  howhy  阅读(3)  评论(0编辑  收藏  举报