Archive

Posts Tagged ‘Function Internals’

arguments.callee – javascript useful property

March 23rd, 2011 prakash No comments

The arguments object is an array like object that contains all of the arguments that were passed into the function. Though its primary use is to represent function arguments, the arguments object also has a property named callee , which is a pointer to the function that owns the arguments object. Consider the

following classic factorial function done with arguments.callee property:


function factorial(num){
     if (num < = 1) {
          return 1;
     } else {
          return num * arguments.callee(num-1)
     }
}

There is no longer a reference to the name “ factorial ” in the function body, which ensures that the recursive call will happen on the correct function no matter how the function is referenced.