Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
composition over inheritance
const barker = (state) => ({
bark: () => console.log('Woof, I am ' + state.name)
})
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
})
const murderRobotDog = (name) => {
let state = {
name,
speed: 100,
position: 0
}
return Object.assign(
{},
barker(state),
driver(state)
)
}
const bruno = murderRobotDog('bruno')
bruno.bark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment