Handling optional parameters in javascript

Viewed 118497

I have a static javascript function that can take 1, 2 or 3 parameters:

function getData(id, parameters, callback) //parameters (associative array) and callback (function) are optional

I know I can always test if a given parameter is undefined, but how would I know if what was passed was the parameter or the callback?

What's the best way of doing this?


Examples of what could be passed in:

1:

getData('offers');

2:

var array = new Array();
array['type']='lalal';
getData('offers',array);

3:

var foo = function (){...}
getData('offers',foo);

4:

getData('offers',array,foo);
12 Answers
Related