Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active January 24, 2017 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electerious/eec8958ca0fd1ee97c1d to your computer and use it in GitHub Desktop.
Save electerious/eec8958ca0fd1ee97c1d to your computer and use it in GitHub Desktop.
Toggle between true and false
const toggler = (initState) => {
initState = initState!==false
let state = initState
return {
get : () => state,
reset : () => state = initState,
toggle : () => state = !state,
on : () => state = true,
off : () => state = false
}
}
@electerious
Copy link
Author

electerious commented Nov 24, 2015

Example:

let t = toggler(true)

t.off() // false
t.on() // true
t.on() // true
t.toggle() // false
t.toggle() // true
t.reset() // true
t.get() // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment