2.1. No extra variables: We can make do by using the fn variable itself as a flag. After calling fn(), we set the variable to null. Before calling fn(), we check that it's not null:
const once = fn => {
return (...args) => {
fn && fn(...args);
fn = null;
};
};
2.2. Alternating functions: In a manner similar to what we did in the previous question, we call the first function and then switch functions for the next time. Here, we used a destructuring assignment to write the swap in a more compact manner. For more information, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Swapping_variables:
const alternator = (fn1, fn2) => {
return (...args) => {
fn1(...args);
[fn1, fn2] = [fn2, fn1];
};
};
2.3. Everything...