Recursively call an anonymous function without using arguments.callee
3 answers
Just name it. In this context, the function will not be available outside the parentheses, so you won't pollute the namespace.
(function recursive(n) {
console.log(n = n - 1);
if (n > 0) {
recursive(n); // just an example
}
})(20);
//recursive(20); // won't be able to call it here...
+4
source to share
From a purely theoretical point of view, if you really want to avoid any names, you can use "Y combinator", which is basically a way to call a function with itself as an argument:
(function (le) {
return (function (f) {
return f(f);
}(function (f) {
return le(function (x) {
return f(f)(x);
});
}));
})(function (f) {
return function (n) {
document.write(n + " ");
if (n > 0) f(n - 1);
}
})(20);
Here's a good explanation on how it works.
+2
source to share