Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Created June 11, 2019 13:58
Show Gist options
  • Save mattlockyer/ecfc98648bbf451ee8a1e9d9d3f780dd to your computer and use it in GitHub Desktop.
Save mattlockyer/ecfc98648bbf451ee8a1e9d9d3f780dd to your computer and use it in GitHub Desktop.
const canBark = (state, howManyTimes) => {
state.barks += howManyTimes
}
const canEat = (state, howMuch) => {
state.food -= howMuch
}
const dog = (name) => {
let state = {
name,
barks: 0,
food: 100,
}
return Object.assign(state, {
bark: (...args) => canBark(state, ...args),
eat: (...args) => canEat(state, ...args)
})
}
const alfie = dog('Alfie')
alfie.bark(3)
alfie.eat(6)
console.log(alfie.barks) //3
console.log(alfie.food) //94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment