Visit Mozilla.org

Core JavaScript 1.5 Reference:Functions:arguments:callee

From MDC


Contents

[edit] Summary

Specifies the currently executing function.

Property of arguments; Function.arguments (deprecated)
Implemented in: JavaScript 1.2

JavaScript 1.4: Deprecated callee as a property of Function.arguments, retained it as a property of a function's local arguments variable.

ECMA Version: ECMA-262

[edit] Description

callee is a property of the arguments local variable available within all function objects; callee as a property of Function.arguments is no longer used. (Function.arguments itself is also deprecated.)

arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.

The this keyword does not refer to the currently executing function. Use the callee property to refer to a function within the function body.

[edit] Examples

[edit] Example: Using arguments.callee in an anonymous recursive function

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. (Anonymous functions can be created by a function expression or the Function constructor.) This is where arguments.callee comes in.

The following example defines a function, which, in turn, defines and returns a factorial function.

function makeFactorialFunc() {
   alert('making a factorial function!');
   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)