Created
October 5, 2015 18:43
-
-
Save Jiert/47ca676baec805755fe9 to your computer and use it in GitHub Desktop.
Composition using factory functions
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
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