It has been pointed out by others that passing default parameters in JavaScript is not possible - a separate function must be created instead.
Nevertheless, there are helpful helper functions available to automatically create closures for you. One of my favorite patterns is utilizing partial function application, where the "default" parameters are positioned on the leftmost side.
For those using modern browsers, the use of Function.prototype.bind is recommended (it also handles the this
parameter which allows passing methods as callbacks).
anotherFunction( callback.bind(undefined, 1) );
// sets the first parameter to 1
// when the callback is executed, the subsequent parameters are filled accordingly
If support for older browsers is essential, it is feasible to create your own partial application functions (many JS frameworks offer some form of this feature, the following example is adapted from Prototype)
Function.prototype.curry = function() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function() {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};