3 ms·
A function can reference itself without defining the name twice. These both work just fine: var factorial1 = function(n) { return n < 1 ? 1 : n * factorial
by Aaronneyer 13y ago
A 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