Composition over Inheritance

Run Settings
LanguageJavaScript
Language Version
Run Command
const barker = (state) => ({ bark: () => console.log(`Whoof, I am ${state.name}`) }) const driver = (state) => ({ drive: () => { state.position += state.speed console.log(`${state.name} drived from position ${state.position - state.speed} to position ${state.position}`) } }) const killer = (state) => ({ kill: () => console.log(`${state.name} goes killing!!!`) }) const murderRobotDog = (name) => { let state = { name, speed: 100, position: 0 } return Object.assign( { getPosition() { console.log(`${state.name} is on position ${state.position}`) } }, barker(state), driver(state), killer(state) ) } const penny = murderRobotDog("Penny") penny.bark() penny.getPosition() penny.drive() penny.getPosition() penny.kill()
Editor Settings
Theme
Key bindings
Full width
Lines