Created
January 22, 2016 22:24
-
-
Save a-double/2bdcc2848496c655f648 to your computer and use it in GitHub Desktop.
JS Composition
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 puncher = ( state ) => ({ | |
| punch: () => console.log( `My name is ${ state.name } and I can punch.` ); | |
| }); | |
| const kicker = ( state ) => ({ | |
| kick: () => console.log( `My name is ${ state.name } and I can kick.` ); | |
| }); | |
| const fighter = ( name ) => { | |
| let state = { | |
| name, | |
| anotherThing: 'stuff', | |
| aThirdOne: 'moar' | |
| }; | |
| return Object.assign( | |
| {}, puncher( state ), kicker( state ) | |
| ); | |
| }; | |
| const kickPuncher = fighter( 'Kick Puncher' ); | |
| kickPuncher.kick(); // My name is Kick Puncher and I can kick. | |
| kickPuncher.punch(); // My name is Kick Puncher and I can punch. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment