Please join me at my new location bryankyle.com

Wednesday, October 10, 2007

arguments.callee

Javascript Tip: Ever wonder what arguments.callee is for? Well, it contains the function being evaluated, so you can write anonymous recursive functions like the following:
(function(n) {
  if (n > 1)
     return n * arguments.callee(n-1);
  else
     return 1;
})(5);
Which is the same as the following written in a more traditional way:
function fact(n) {
   if (n > 1)
      return n * fact(n-1);
   else
      return 1;
}

fact(5);