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.

4

3 回答 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.

于 2013-03-30T15:23:47.490 回答
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.

于 2013-03-30T15:23:06.197 回答
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.

于 2013-03-30T15:40:05.343 回答