I was wondering, can you create a function with an optional parameter.
Example:
function parameterTest(test)
{
if exists(test)
{
alert(\'t
init default values if not exists:
function loadDialog(fn, f, local, anim) {
switch (arguments.length) {
case 1: f=null;
case 2: local=false;
case 3: anim=false;
}
...
}
In JavaScript, if you neglect to give a parameter it will default to undefined
.
You could try it out for yourself easily enough, either in your browser console or using JSFiddle.
You can check for the existance of the parameter, as you say, and that way write a function that can use a parameter or not. However, JavaScript Garden (a great resource) recommends staying away from typeof
in most other cases, as its output is just about useless (check out the table of results of typeof).