Created
June 12, 2017 16:36
-
-
Save maxpou/736f372c19dd01b6e3ca88b73c9ef78a to your computer and use it in GitHub Desktop.
composition over inheritance
This file contains 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 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