Yes, fn.length
returns what JS can discern as a function's arity. But since a function can include optional args, rest args or rely on the arguments
object, the length
cannot be trusted and should be used only in isolated circumstances.
For example, I implement a curry
function in my lib which uses length
by default, but the overload (e.g. curry(map, 2)
) allows me to override the arity.
And that's the crux of it. The seeming usefulness of length
is how it can be used to compose higher-order functions, but since length cannot be trusted, said higher-order function must allow an override of the default.
Since you're dealing with partial application and rest args (totally confounding what an acceptable length might be) and a host of compositions transforming one function into another, you can't trust length
. This is further complicated by the possibility the function is overloaded and supports multiple arities.
When it comes to any given function the programmer (not the runtime) must have an intimate knowledge of its arity(ies).
The short of it: length
is too unreliable for practical use that it ought not have been provided in the first place.