4 ms·
The function is able to reference itself, allowing recursion. This used to cause an error in Safari though, am I right?
by jbrooksuk 13y ago
The function is able to reference itself, allowing recursion. This used to cause an error in Safari though, am I right?
- aaronem 13y agoThat doesn't seem special: var foo = function (n) { return foo(n); }; foo(1); gives back "too much recursion", not "foo is not defined", in both Spidermonkey (Firefox 24) and V8 (node.js 0.10.something).
- Aaronneyer 13y agoA function can reference itself without defining the name twice. These both work just fine: var factorial1 = function(n) { return n < 1 ? 1 : n * factorial1(n-1) } function factorial2(n) { return n < 1 ? 1 : n * factorial2(n-1) }
- jrawlings 13y agoRecursive functions like this are more like closures and not pure functions. Aliasing factorial1 and reassigning it will break the recursion [1]. Maybe this is what jbrooksuk was referring to. [1] https://leanpub.com/javascript-allonge/read#recursive https://leanpub.com/javascript-allonge/read#recursive