javascript - How can I execute recursion regardless of the function name? -
possible duplicate:
can name of running function in javascript?
i need recursion. i'd able change function name without checking , changing every instance function calls itself.
(for example php has "magic constants" __function__
etc.. - similar in javascript?)
if goal recursion, don't need function name string if can reference function itself. note you'll still have check , change every instance function called others, , that's larger task updating recursive calls (if no other reason it's not localized function body).
passing function argument
function foo(self, i) { if (i === 0 ) return; console.log(i--); self(self, i); } foo(foo, 3);
using arguments.callee (as raynos points out, brittle in strict mode)
function foo(i) { if (i === 0 ) return; console.log(i--); arguments.callee(i); } foo(3);
output of either option
3 2 1
Comments
Post a Comment