Default arguments in JavaScript

I sometimes run into people who need (whatever the reason may be) to be able to give default argument values in a JavaScript function. Similar to what you find in C++:

void foo(int a, int b = 42) {
//b is set to 42 automatically if no value is defined
...
}

I felt an urge to do it, so I sat down for a few minutes, trying to conjure something nifty that would be intuitive enough for even me to use.

The solution I came up with, and that hopefully will put an end to the default-arguments rant for good.

The solution is quite simple, and very intuitive. It may have some shortcomings but keep in mind I didn’t explicitly write it for you. You could probably modify my solution to fit your needs anyway.

I’ve seen a lot of developers (including myself, long time ago) using this pattern as a workaround for the lack of built-in support for default-arguments.

function foo(a, b)
{
a = typeof(a) != 'undefined' ? a : 42;
b = typeof(b) != 'undefined' ? b : 'default_b';
...
}

Which most developers probably find sufficient, I don’t.

The solution may look scary at a quick first glance, but bear with me. I’ll start by writing the framework-code. The code that will be required for this thing to work.

Function.prototype.defaults = function()
{
var _f = this;
var _a = Array(_f.length-arguments.length).concat(
Array.prototype.slice.apply(arguments));
return function()
{
return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(
_a.slice(arguments.length, _a.length)));
}
}

See that wasn’t so scary :) .

In order for this to work you have to declare you functions in a special way. There are basically two ways of declaring a function.

function foo(a, b)
{
...
}

Is identical to (apart from the lexical difference, and some other minor things)

var foo = function(a, b)
{
...
}

In order for this solution to work you have to declare all functions on which you wish to have default-arguments the latter way.

Usage

var foo = function(a, b)
{
...
}.defaults(42, 'default_b');

Is identical to the first code-block but without adding any code in the function-body.

Example

var bar = function(a, b)
{
}.defaults('default_b');
bar();
// a = undefined, b = 'default_b'
bar(1);
// a = 1, b = 'default_b'
bar(1, 'some_value');
// a = 1, b = 'some_value'

Finally, a decent JS Tutorial!

Essential Javascript - A Javascript TutorialThe lack of a proper JavaScript tutorial on the web is well known to the members of this channel. We’ve even tried our own half-arsed versions once or twice. But I was pleasantly surprised to find Essential Javascript - A Javascript Tutorial.

This tutorial is clean and concise, covering basics such as variables, functions, events, math and dates. A nice feature is the code doesn’t look like it dates to 1998. The tutorial is actually several tutorials, and also covers popular topics such as AJAX for Noobs and JSON. An excellent resource for those just learning the language or eager to dive into more advanced topics but lacking fundamentals.