Fwio

Function.prototype.length

Function.prototype.length返回函数的形参(parameters)个数。

这在 JS 的函数柯里化(currying, or partially applied function)实现中会用到。

function curry<T>(func: (...args: any[]) => T) {
  const appliedArgs = []

  const curried = (...args: any[]) => {
    appliedArgs.push(...args)
    if (appliedArgs.length >= func.length) {
      return func(...appliedArgs)
    } else {
      return curried
    }
  }

  return curried
}