Skip to content

Instantly share code, notes, and snippets.

@a-double
Created January 22, 2016 22:24
Show Gist options
  • Select an option

  • Save a-double/2bdcc2848496c655f648 to your computer and use it in GitHub Desktop.

Select an option

Save a-double/2bdcc2848496c655f648 to your computer and use it in GitHub Desktop.
JS Composition
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