Wednesday, August 18, 2010

Recursion of anonymous functions

When I declare anonymous function, like

(function(){
//do something...
})();
name of the function is not added to any scope, either local or global, because there is no name at all. When I want to call the function again, for example in timer functions like setTimeout(), I simply call arguments.callee inside it

(function(){
//do something...
setTimeout(arguments.callee, 1000);
})();
According to Mozilla Developers Center:
arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.

3 comments: