method overloading in javascript

2007-11-13 @ 18:36#

johnR's post on supporting method overloading in javascript is sweet!

he takes advantage of a little-known feature in javascript. that functions have a length property that indicates the number of arguments they accept.

below is the summary method. check out the link for more.

   // 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 );