When writing a simple factorial function in its recursive form, we usually just call the function within itself by its own name. This can cause issues if the function is then assigned to some other name and is called via that reference while the original reference is no longer valid, like in the following code.
var factorial = function (number) {
if (number < 1) {
return 1;
} else {
return number * factorial(number - 1);
}
};
var fact = factorial;
factorial = 5;
console.log(fact(10));
Now, the obvious fix to this is to call the function inside via arguments.callee
but that is invalid in strict mode. Also arguments.callee is not available for arrow functions in ES6.
So my question is essentially this. What is the best practice in writing recursive functions that are decoupled from their name? Also, how do I write an arrow function in ES6 recursively?