Created
June 11, 2019 14:03
-
-
Save mattlockyer/d5c0b4087b70332f389b713fe5e0a575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canBark = (state) => ({ | |
bark:(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, canBark(state), { | |
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