Design & Download
Your JS Library
in Seconds!!!

Snippet: cap() - Limit Function Argument Count

Written by Christopher West (cwest) on November 09, 2015.
Create a function which will only handle up to a certain amount of arguments. All additional arguments will be ignored.
function cap(fn, opt_maxArity) {
  opt_maxArity = opt_maxArity != undefined ? opt_maxArity : fn.length;
  return function() {
    return fn.apply(this, slice(arguments, 0, opt_maxArity));

cap(fn [, opt_maxArity=fn.length]) API Documentation


  1. fn {Function}:
    The function whose argument count will be capped off (limited).
  2. opt_maxArity {number}:
    Optional. Defaults to fn.length. The max number of arguments that will be passed into fn.


Returns a function that will call fn with the passed in arguments when invoked, but it will limit the argument count to the value opt_maxArity. When calling this newly created function, it will behave like this: fn.apply(this, YourJS.slice(arguments, 0, opt_maxArity)).