Skip to content

Instantly share code, notes, and snippets.

@Jiert
Created October 5, 2015 18:43
Show Gist options
  • Save Jiert/47ca676baec805755fe9 to your computer and use it in GitHub Desktop.
Save Jiert/47ca676baec805755fe9 to your computer and use it in GitHub Desktop.
Composition using factory functions
var barker = function(state){
return { bark: function(){ console.log('Woof, I am ' + state.name); } };
};
var driver = function(state){
return { drive: function(){
state.position = state.position + state.speed;
console.log(state.position);
} };
};
var murderRobotDog = function(name){
var state = {
name: name,
speed: 100,
position: 0
};
return Object.assign(
{},
barker(state),
driver(state)
);
};
var bucky = murderRobotDog('bucky');
var goose = murderRobotDog('goose');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment