SlightlyLoony
Tera Contributor

find_real_file.pngIn an earlier post, I touched a bit on the arguments property that's available in any function. There I showed how you can use it to create functions that take a variable number of arguments. Today I'm going to dive into a bit more detail on that arguments property, including one little bit that just might qualify as the most obscure part of JavaScript: the callee property.

The two female cardinals at right got into a squabble over this, but we don't need to...

Here's a bit of code that illustrates all the things I want to talk about:


function factorial(x) {
return x ? x * factorial(x - 1) : 1;
}

var f = function(x) {
return x ? x * arguments.callee(x - 1) : 1;
};

var g = function() {
return arguments[0] ? arguments[0] * arguments.callee(arguments[0] - 1) : 1;
};

function identity(x) {
gs.log('Identical? ' + ((x === arguments[0]) ? 'yes' : 'no'));
}

gs.log(factorial(8));
gs.log(f(8));
gs.log(g(8));
identity('bogus');

And when I run the code, I get this log:

40320
40320
40320
Identical? yes

The factorial function uses recursion to compute the factorial of the given number. This function is defined in the usual manner.

The next example is the same recursive factorial function, but it is defined anonymously and assigned to the variable f. That leads to a problem: how can the anonymous function (which of course has no name) call itself recursively? The callee property (of the arguments property) solves this menace to our programming freedom. It is always set to the function itself — exactly what you need to make a recursive call. So far as I'm aware, that's the only use of the callee property.

Right after that, we do almost exactly the same thing: define the same recursive factorial function, anonymously. But this time we didn't even name an argument — instead, we took the argument from the arguments property, by specifying the first argument (arguments[0]). Otherwise it works exactly the same way.

The last part of the example is the identity function. Here we're just illustrating a special feature of the arguments property. It works basically like an array, but there are some differences. One of those differences is that the numbered properties of the arguments property are synonyms for the named arguments to the function. In the identity function we compare the named argument x to the arguments[0] property using the JavaScript strict equals operator — and we find that they are exactly the same value, confirming that the named argument and the numbered element of the arguments property are just two different ways to refer to exactly the same value.

Right about now you might be asking yourself what you're going to do with this useless dreck I've just filled your mind with. Well, you can impress your geekly friends with your knowledge of obscure JavaScript trivia. Or you could confuse your enemies by creating all your JavaScript functions without any named arguments at all. Oh, and there's always the remote possibility that you might actually need to create a recursive anonymous function with a variable number of arguments. You might, you never know...