Javascript arguments.callee

All functions in Javascript are instances of the Function type, meaning that they are actually objects. And just like any other object in Javascript, they can have their own set of properties and methods. One such property is the arguments object, which contains a list of all the parameters passed in to the function. The arguments object itself contains a callee property, which points to the function that owns the arguments object. This provides us with a way to reference the function being called while inside of the function body itself.

Why would you want to do this? Well, this can come quite in handy if we want to define a static variable scoped to the function itself. Perhaps the function contains code that performs a particularly complex calculation or an expensive initialization that only needs to be run once. We can store the result inside of a static variable which will improve response time on subsequent calls to this function. For example:

Function createFoo()
{
   If (typeof arguments.callee.bar != “number”) 
   {
       Arguments.callee.bar = ….  //perform expensive logic here		
   }

   //now we can refer to arguments.callee.bar without having to perform
   //the expensive logic again on subsequent calls
}

Leave a Reply

Your email address will not be published. Required fields are marked *