I'm writing a function that takes between one and three arguments (function callback [, number limit] [, regexp pattern])
. Since the second two are both optional, I need to determine which arguement is being passed when the function is supplied with only two. Below is my solution, but it seems overly verbose. Is there a cleaner way to do this?
var get_queued_pages = function() {
if(arguments>0){
var callback = arguments[0];
if(arguments.length == 3){ //all arguments are being passed
var limit = arguments[1];
var pattern = arguments[2];
}
else if(arguments.length == 2){ //only 2 arguments passed
if(typeof(arguments[1]) == "number"){ //check if the second one is limit or pattern
var limit = arguments[1];//no pattern defined; second argument is limit
}
}
else{
var limit = 1;
}
}
//do stuff
}