For the user's ease, I have a function that receives an optional argument first, before the required argument. For example:
ns.myFunction('optional arg', function(){
//the required callback
});
I'm doing this rather than doing the following since the callback body could be long, and the user might forget to override the defaults to the optional arguments:
ns.myFunction(function(){
//the required callback
}, 'optional arg');
Currently I'm doing this to check:
function myFunction(first, second) {
//if second is undefined and first is a function
if (typeof second === 'undefined' && typeof first === 'function') {
second = first;
}
}
Questions
- Is this the right way?
- How would I do it so that it scales, especially if there were N optional arguments that are before the required argument?
.bind(). The more "stable" arguments should come first, with the ones more likely to vary or be absent at the end.arguments[..]? Wouldn't iterating through the array, and stopping at the firsttypeof arguments[i] === 'function'help?.extendfunction is kind-of a special case in that it has two kinds of optional arguments; one could argue that it'd be better for it to be two separate functions, but arguments like that are silly :-)