Last week I was discussing with my pals @drslump and @ladybenko about a very simple idea I came up with while reading John Resig [Secrets of the JavaScript Ninja] ( book on Christmas holidays.

The idea is very simple: ensure that a function is called with the expected number of parameters.

A function defined like:

var fullName = function fullName (name, surname) {
    return name + ' ' + surname;

could be called with:

  • zero parameters: name and surname will be undefined.
  • one parameter: the parameter will be assigned to name, surname will be undefined.
  • two parameters: the former parameter will be assigned to name, the latter to surname.
  • three or more parameters: the first two parameters will be assigned to name and surname respectively, the next ones could be accessed via arguments.

You might want to ensure the function is always called with two parameters, so scenarios like this one won’t happen:

'Foo undefined'

Let’s do the magic by defining a method in the Function prototype:

Function.prototype.validateArity = function validateArity () {
    var fn = this;
    return function () {
        if (arguments.length === fn.length) {
            return fn.apply(this, arguments);
        } else {
            throw new Error("Arity was <"+arguments.length+"> but expected <"+fn.length+">");

Simply adding to our previous function the following:

var fullName = function fullName (name, surname) {
    return name + ' ' + surname;

// Correct call
console.log(fullName("Foo", "Bar"));
Foo Bar

// Incorrect call
Error: Arity was <1> but expected <2>

Protip: adding a function to Function prototype is usually NOT a good idea.

« Home