DEV Community

Discussion on: The obscure `Function#length` property!

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Not that obscure - I was using it in my project just last week!

I had two different functions to run for attaching methods to objects - one for methods that took arguments, another for methods that didn't. The Function length property was very useful to create a convenience function to automatically pick the correct method adder.

It is also used in the commonly used function that will curry the passed function:

function curry(func) {

  return function curried(...args) {
    if (args.length >= func.length) {
      return func.apply(this, args);
    } else {
      return function(...args2) {
        return curried.apply(this, args.concat(args2));
      }
    }
  };

}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lioness100 profile image
Lioness100

Oooh, genius! (And the project that you linked is super cool too!)