JavaScript: overloading methods inside objects

Back in 2007, John Resig published this interesting post on JavaScript method overloading. As he says, it's just a way of mapping a single function call to multiple functions based upon the arguments they accept. After viewing his examples, I started wondering how this method could be included within the namespace of a pre-existing object. This method is as follows:

// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
        if ( fn.length == arguments.length )
            return fn.apply( this, arguments );
        else if ( typeof old == 'function' )
            return old.apply( this, arguments );

This method accepts three arguments:

  1. object: an object where methods should be added
  2. name: the name of the method
  3. fn: an anonymous function that is actually the body of our method.

First, addMethod() stores a reference to the target object's method in the private old variable. Then it creates the body of the new method with an anonymous function. This function checks whether the arguments of the new method and the arguments of the addMethod() method have the same length. If so, it calls apply() on the new function. Otherwise, it calls apply() on the old reference. Some examples:

function Users(){
  addMethod(this, "find", function(){
    // Find all users...
  addMethod(this, "find", function(name){
    // Find a user by name
  addMethod(this, "find", function(first, last){
    // Find a user by first and last name

Practical usage:

var users = new Users();
users.find(); // Finds all
users.find("John"); // Finds users by name
users.find("John", "Resig"); // Finds users by first and last name
users.find("John", "E", "Resig"); // Does nothing 

This method is global. A way to reference it within an object would be creating a private method:

function Class() {

  var that = this;
  function addMethod (...) {
  addMethod(that, 'show', function() {...});


However, I have to test it extensively.

Leave a Reply

Note: Only a member of this blog may post a comment.