4

In C# I might have an overloaded function like so:

public string OverLoad(string str1)                  {return str1;}
public string OverLoad(string str1, string str2)     {return str1+str2;}

I know that JavaScript does not have built in support for overloaded functions, but does anyone work around this to still achieve overloaded function capabilities?

I am curious about:

1.) Effect on speed

2.) Reasons to use different function names vs. overloads

It seems you could check for the parameters being undefined or not and "fake" the capability in JavaScript. To me the ease of changing functions by only changing the number of parameters would be quite an advantage.

1
  • 1
    JavaScript cannot overload functions - they are variadic. Yet you can use this more powerful concept to emulate overloaded function; see also method overloading in Javascript Commented Mar 30, 2013 at 15:25

3 Answers 3

4

In javascript there is a tendency to pass parameters are attributes of an object, ie there is one parameter which is an object, and thus you can specific anything, in any order. With this approach you can check for 'undefined's, and your function can behave according.

If you have rather large behaviour in different cases, you could extend this idea by splitting each case into a seperate function, which is then called by your main 'overloaded' function.

For example:

function a (params) {
    // do for a
}
function b () {
    // do for b
}
function main (obj) {
    if (typeof obj.someparam != 'undefined') {
        a(whatever-params);
    } else if (typeof obj.someotherparam != 'undefined') {
        b(whatever-params);
    }
}

You could also use this same approach based on the number parameters, or number of arguements. You can read about using the arguments here.

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't answer the OP's question for speed concerns. Could you add something about that?
2

JavaScript does not have a support for function overloading. Although it allowed you to pass a variable number of parameter.

You have to check inside function how many arguments are passed and what are their types.

1 Comment

Yes I realize there isn't built in support, wondering if users extend JS functionality via work arounds to achieve this on their own and what the reasons behind doing that might be.
2

This is so common that it is not even noted.

E.g., in JQuery, the on() method has multiple call signatures:

.on( events [, selector ] [, data ], handler(eventObject) )
.on( events [, selector ] [, data ] )

A call to the first form looks like $('sel').on('click', function(e){/*handler*/}). A call to the second form looks like $('sel').on({click: function(e){/*click handler*/}, mouseover: function(e){/*mouseover handler*/}). Note that this demonstrates both "polymorphism" (i.e. arguments of different types) and "overloading" (i.e. optional arguments) in the Java/C# terminology.

Keyword arguments (simulated using a single object argument) are also common, e.g. $.ajax({settings}).

So yes, this is done all over the place, but your referring to this as "overloading" is a category confusion. JS is neither staticly typed nor do its functions have a fixed number of function arguments. At call time you can supply as many or as few arguments as you want, regardless of what the function declares. You should view the argument list in the function declaration as a shorthand for array destructuring of the arguments magic variable rather than as a fixed function signature.

The effect on speed is so minimal as to not even be a consideration.

As for when to do this vs when to use a separate function, that is entirely a program clarity and usability concern and probably subject to wide opinion.

1 Comment

Incredible answer Francis. I've only had limited exposure to statically typed languages so I wasn't aware of what actually classified a function as "overloaded," just the general premise of how these functions operate. After more research I agree I am making a category confusion mistake, as I was definitely thinking in terms of a fixed function signature. Thanks, you're the man.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.