[X]

How Anonymous Functions Recursed

luljs still works

Before a function needed a name to call itself, it reached itself through arguments.callee.

In 2026: Works in a plain script, banned in strict mode and modules. arguments.callee points at the running function, so an unnamed function could call itself with it. Strict mode threw it out because it blocked optimisation and leaked the function, so the modern answer is to give the function a name and call the name. The demo runs sloppy, so it still works.

Where it came from: arguments.callee and its strict-mode ban, documented on MDN. MDN

<script>
var factorial = function (n) {
  return n <= 1 ? 1 : n * arguments.callee(n - 1);   // the function calls itself with no name
};
document.write('<p><code>factorial(5)</code>, recursing through <code>arguments.callee</code>, is <b>' + factorial(5) + '</b></p>');
document.write('<p>In strict mode that line throws. The fix is a named function expression.</p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›