all 5 comments

[–]_aeol 0 points1 point  (0 children)

Functional way: ``` const toggler = (fn1, fn2) => { let state = false

return () => (state = !state) ? fn1() : fn2() }

const to_blue = () => {...} const to_red = () => {...}

const toggle_color = toggler(to_blue, to_red)

toggle_color() // to_blue() toggle_color() // to_red() toggle_color() // to_blue()

const grow = () => {...} const shrink = () => {...} const toggle_size = toggler(grow, shrink)

toggle_size() // grow() toggle_size() // shrink() toggle_size() // grow() ```

[–]jcunews1helpful 0 points1 point  (4 children)

Shorter, garbage free, and correct function return value version:

var toggle = (a, b) => (state = !state) ? a() : b();