Recursive Anonymous Function
arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.
A recursive function must be able to refer to itself. Typically, a function refers to itself by its name. However, an anonymous function does not have a name, and if there is no accessible variable referring to it, i.e. the function is not assigned to any variable, the function cannot refer to itself. This is where arguments.callee comes in.
The following example defines a function, which, in turn, defines and returns a factorial function:
function makeFactorialFunc() {
return function(x) {
if (x <= 1)
return 1;
return x * arguments.callee(x - 1);
};
}
var result = makeFactorialFunc()(5); // returns 120 (5 * 4 * 3 * 2 * 1)
Other interesting usages that I have not fully explore:
- combine arguments.callee with setTimeout, and closure
- combine arguments.callee with apply() in an object oriented environment
page_revision: 0, last_edited: 1224660606|%e %b %Y, %H:%M %Z (%O ago)





