views:

448

answers:

2

I'm quite familiar with jQuery. I'm trying to write common methods for my own purpose. Here is a sample below:

$.extend({
    add  : function(a, b)
           {
             return a + b;
           },

    add  : function(a, b, c)
           {
             return a + b + c;
           }
   });

Is the above scenario possible? Can I use the same extender name and pass different parameters, like method overloading?

+3  A: 

I think that will just overwrite the first 'add' property with the 2nd 'add' property you defined. This is useful when developing plugins, and want to provide a list of sensible defaults for a config object.

alex
+9  A: 

You are trying to do some type of what is called in some languages method overloading.

JavaScript doesn't supports it in that way.

JavaScript is very versatile and lets you achieve this kind of feature in different ways.

For your particular example, your add function, I would recommend you to make a function that accepts an arbitrary number of parameters, using the arguments object.

jQuery.extend(jQuery, {
  add: function (/*arg1, arg2, ..., argN*/) {
    var result = 0;

    $.each(arguments, function () {
      result += this;
    });

    return result;
  }
});

Then you can pass any number of arguments:

alert(jQuery.add(1,2,3,4)); // shows 10

For more complex method overloading you can detect the number of arguments passed and its types, for example:

function test () {
  if (arguments.length == 2) { // if two arguments passed
    if (typeof arguments[0] == 'string' && typeof arguments[1] == 'number') {
      // the first argument is a string and the second a number
    }
  }
  //...
}

Check the following article, it contains a very interesting technique that takes advantage of some JavaScript language features like closures, function application, etc, to mimic method overloading:

CMS
It's late and I'm probably reading your code wrong but wouldn't `jQuery.add(1,2,3,4)` return `10`?
Jared
His function uses a magic pattern parser, and determined the next number in sequence to be 5 ;)
alex
@Jared: You're completely right, just was a typo!
CMS
add: function (/*arg1, arg2, ..., argN*/)How will the above code be?add: function (a,b,c,d,e???) {
engineerachu
@engineerachu: You don't need to declare any input arguments, `/*arg1, arg2, ..., argN*/` is just a comment to let know that this function can accept any number of arguments, in fact it could just be: `add: function (){`
CMS
Oh Okay thanks. Let me try.
engineerachu
My scenario is like, I need to call "copyRow" method where one may have 2 parameter and other may have 3 paramaters. Eg:$.extend({ copyRow : function(obj, chkBoxID) { // Do operation }, copyRow : function(obj, ChkBoxID, count) { // Do operation }});Is the above possible with the method you gave?
engineerachu
@CMS: Your answer is totally right and should be accepted. However, there is one more possibility you have. You could use an object as the one and only argument and that object might contain a different number of properties.
Tim Büthe