Javascript: optional, defaultable function arguments
function doSomething (sWhatever, oOptions) {
var oOptions = augment({
sOptOne: "foo",
bOptTwo: false
}, oOptions );
// sWhatever is a regular argument. We assume it's always going to be passed
alert(sWhatever);
// sOptOne and bOptTwo are optional;
// they are defaulted to "foo" and false, respectively
alert(oOptions.sOptOne);
alert(oOptions.bOptTwo);
}
The function defined above can be called in a number of different
ways:
- doSomething("First value"); -- this invokes the function without either of the optional arguments, so sOptOne and bOptTwo both get their default values ("foo" and false, respectively).
- doSomething("First value",{bOptTwo:true}); -- in this case, the bOptTwo argument is specified in the functional call, so it's value is true, rather than the default value of false.
- doSomething("First value",{sOptOne:"Yahoo",bOptTwo:false}); -- here, both the optional arguments are provided.
The key here is that the optional arguments are specified by name, rather than position. Therefore, you can leave any of them out, and they assume the default value you specified in the function itself.
The augment() Function
The "augment()" function is what makes it all happen. This function takes two or more objects, and augments the first object with properties from the second (or subsequent, of you pass more than one) object(s).
For defaultable arguments, the first object is the default values ( that's the {sOptOne: "foo", bOptTwo: false} part, and the second object is the oOptions object passed into our function.
Here is the augment function:
/*
Augment an object by replacing its key:value pairs with those
from other object(s), and adding pairs from other object(s) that don't
exist in you. Key:value pairs from later objects will
overwrite those from earlier objects.
If null is given as the initial object, a new one will be created.
This mutates and returns the object passed as oSelf. The other objects are not changed.
*/
function augment (oSelf, oOther) {
if (oSelf == null) {
oSelf = {};
}
for (var i = 1; i < arguments.length; i++) {
var o = arguments[i];
if (typeof(o) != 'undefined' && o != null) {
for (var j in o) {
oSelf[j] = o[j];
}
}
}
return oSelf;
}
Optional, defaultable arguments are a powerful Javascript programming technique. They make it much easier to evolve your code, especially when you build "visual" objects -- objects that correspond to visual elements on the page.

Comments
Patrick Fitzgerald on Apr 21
Seems like a lot of work to override the arguments, I just did it like this:
function myfunction() {
var args = { a:1, b:2, c:3 };
for (var n in arguments[0]) { args[n]=arguments[0][n]; }
}
leevi graham on Apr 22
Andre,
Thanks for documenting this handy tip. Im sure it will greatly improve my code.
Well done.
Leevi
Jacob S on Nov 06
Patrick, sure it's true that your method may be easier, but with Andre's, the arguments are passed as an object, and so the ORDER of them doesn't matter. With your method, you have to make sure that you specify all parameters, regardless if you want to use them or not.
Lawrence on Oct 04
Actually, Patrick's method works just like Andre's -- you pass in an object, so like Andre's the order doesn't matter and you only need specify (in your function call) the ones you want to override. This is because the 'for (n in arguments)' block iterates over object properties, not over array items, so Patrick's code is functionally similar to Andre's.
John on Jan 09
Thanks Andre!
I think swapping line 8 with "if(o[j] != null) oSelf[j] = o[j];" helps because you can pass in a value that might equal null and not have it overwrite your original value. I might make that another param to say 'overwrite' or not.