January 2020
Intermediate to advanced
470 pages
11h 13m
English
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 ...Read now
Unlock full access