Method Overloading is possible in JavaScript!

In a full-fledged Object Oriented Programming language such as Java, method overloading is the technique that you could use to create few functions or methods that having the same names but taking different arguments or parameters. Such as follows, the method findUser(), findUser(String), findUser(String, String) in Java.

public class Users
   public Collection findUser()
      // the findUser() is a method that returns
      // all users in a java.util.Collection object


   public Collection findUser(String name)
      // the findUser(String) is a method that returns the users matched
      // by the specified name,
      // result returned in a java.util.Collection object


   public Collection findUser(String firstMame, String lastName)
      // the findUser(String,String) is a method that
      // returns the users matched by the specified
      // first and last name, results returned in a java.util.Collection object


Method overloading in JavaScript
But method overloading used in JavaScript perhaps might sound a bit new to you, but it’s possible, as shown in the example below demoed by John Resig on his post. But in JavaScript, which isn’t a strong-type language as what Java is. So, types of argument are not considered in JavaScript.

2.    // addMethod – By John Resig (MIT Licensed)
3.    function addMethod(object, name, fn){
4.       var old = object[ name ];
5.       object[ name ] = function(){
6.          if ( fn.length == arguments.length )
7.             return fn.apply( this, arguments );
8.          else if ( typeof old == ‘function’ )
9.             return old.apply( this, arguments );
10.          };
11.    }
13.   // Now setup the methods
15.    function Users(){
16.       addMethod(this, “find”, function(){
17.       // Find all users…
18.       });
20.       addMethod(this, “find”, function(name){
21.    // Find a user by name
22.       });
24.       addMethod(this, “find”, function(first, last){
25.    //Find a user by first and last name
26.       });
27.    }
28.    // Now use the methods
29.    var users = new Users();
30.    users.find(); // Finds all
31.    users.find(“John”); // Finds users by name
32.    users.find(“John”, “Resig”); // Finds users by first and last name
33.    users.find(“John”, “E”, “Resig”); // Does nothing

John ends with the caveats:
1. As I mentioned, JavaScript is non-strong-type language, so it only works for different numbers of arguments – it doesn’t differentiate based on types. (But it seems like ECMAScript 4/JavaScript 2, however, will have this ability – called Multimethods)

2. There is overhead involved in method overloading in JavaScript. Do not consider this for apps needing high performance.

The secret falls on the fn.length expression. This isn’t very well known, but all functions have a length property on them. This property equates to the number of arguments that the function is expecting. Thus, if you define a function that accepts a single argument, it’ll have a length of 1, like so:

(function(foo){}).length == 1

via [Ajaxian]

Enter your email address to subscribe our newsletter or feed for FREE:

Delivered by FeedBurner

Bookmark with:

[Delicious]    [Digg]    [Reddit]    [Facebook]    [StumbleUpon]

0 Responses to “Method Overloading is possible in JavaScript!”

  1. No Comments

Leave a Reply

You must login to post a comment.