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?
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:
and if the function is called with only 2 parameters,
onSuccesswill beundefined:You may want to look at
jQuery.Callbacksfor handling event callbacks, although your function could be rewritten using.load.For programmers coming from the Java/C# world of overloading
learn the JavaScript wayyou can use a structure like this (not recommended, it’s simply an example for familiarity):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
optionsparameter will be used to contain the available parameters: