Johan
Johan

Reputation: 35194

overloaded methods

function Activity() {

this.LoadFile = function (path, targetElement) {

    $.ajax({
        url: path,
        dataType: 'html',
        success: function (data) {
            targetElement.html(data);
        }

    });
};

this.LoadFile = function (path, targetElement, onSuccess) {

    $.ajax({
        url: path,
        dataType: 'html',
        success: function (data) {
            targetElement.html(data);
            onSuccess(data);
        }

    });
};

}

If i try to pass 2 arguments, i get onSuccess is not a function so i suppose this isnt working. Is it possible to use overloads in javascipt?

Upvotes: 1

Views: 242

Answers (3)

yunzen
yunzen

Reputation: 33439

There is still another way to go. Don't give any parameters in the function definition and then examine the arguments array(). You can even examine the types of the parameters given, to branch inside your function. The example below will alert # 1 # and Num(2)

/** 
 * Usage
 * example(number param1) 
 * example(string, number)
 */
function example() { // no parameters
    if (arguments.length == 1 && typeof arguments[0] == 'number') {
        alert(' # ' + arguments[0] + ' # ')
    }
    if (arguments.length == 2 && typeof arguments[0] == 'string' && typeof arguments[1] == 'number') {
        alert(arguments[0] + '(' + arguments[1] + ')')
    }

}
example(1);
example('Num', 2)

Upvotes: 0

zzzzBov
zzzzBov

Reputation: 179046

There's no way of directly specifying overloaded methods as such in JS.

There are many workarounds.

In your case, you can define the function with 3 parameters:

function (path, targetElement, onSuccess)

and if the function is called with only 2 parameters, onSuccess will be undefined:

function (data) {
  targetElement.html(data);
  if (onSuccess) {
    onSuccess(data);
  }
}

You may want to look at jQuery.Callbacks for handling event callbacks, although your function could be rewritten using .load.


For programmers coming from the Java/C# world of overloading learn the JavaScript way you can use a structure like this (not recommended, it's simply an example for familiarity):

function foo() {
  var fns = {};
  fns[0] = function () {
    //no params
  };
  fns[1] = function () {
    //1 param
  };
  fns[2] = function () {
    //2 params
  };
  fns[arguments.length]();
}

to provide separate functions for each "overload", however it's not very flexible and symptomatic of a poorly planned function.


Oftentimes rather than using a large set of parameters, a single options parameter will be used to contain the available parameters:

function foo(options) {
  var settings = $.extend({}, foo.defaultSettings, options);
  //do stuff with settings
}
foo.defaultSettings = {
  bar: 'baz',
  fizz: 'buzz'
}

Upvotes: 2

SLaks
SLaks

Reputation: 887405

No.
Javascript does not support method overloading.

Instead, you can make a single method that takes all three parameters, then check inside the method whether the third parameter was actually passed. (all Javascript function parameters are optional)

Upvotes: 0

Related Questions