Skip to content

Instantly share code, notes, and snippets.

@Jiert
Created October 5, 2015 18:22
Show Gist options
  • Save Jiert/7c5b3a1cf81145a2392d to your computer and use it in GitHub Desktop.
Save Jiert/7c5b3a1cf81145a2392d to your computer and use it in GitHub Desktop.
Composition Example
const barker = (state) => ({
bark: () => console.log('Woof, I am ' + state.name)
})
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
})
barker({ name: 'karo'}).bark()
// Woof, I am karo
const murderRobotDog = (name) => {
let state = {
name,
speed: 100,
position: 0
}
return Object.assign(
{},
barker(state),
driver(state),
killer(state)
)
}
murderRobotDog('sniffles').bark();
// 'Woof I am sniffles'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment