Last active
October 2, 2015 18:25
-
-
Save clalimarmo/ad5f6f7605f24fa9d90f to your computer and use it in GitHub Desktop.
How things are done everywhere. Versus how I want them to be done.
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
// tired of this extension pattern | |
var Cow = AnimalFactory({ | |
getVoice: function() { return 'moo'; }), | |
numLegs: 4, | |
name: 'Cow', | |
}) | |
// propose this one instead | |
var MutantCow = AnimalFactory.extend() | |
.getVoice(function() { return 'moo'; }) | |
.numLegs(5) | |
.name('Cow'); | |
var Crow = AnimalFactory.extend() | |
.getvoice(function() { return 'CAWW!'; } // no method error | |
.numlegs(2) // no method error | |
// what other things can I configure? | |
// the configuration object gives me a good hint | |
Object.keys(AnimalFactory.configure()) | |
function AnimalFactory() { | |
var classConfig = {}; | |
var = {}; | |
var _classAttributes = {}; | |
//downside: lots of boilerplate for the owner | |
classConfig.getVoice = function(voiceFunction) { | |
_classAttributes.getVoice = voiceFunction; | |
return classConfig; | |
}; | |
classConfig.numLegs = function(numLegs) { | |
_classAttributes.numLegs = numLegs; | |
return classConfig; | |
}; | |
classConfig.name = function(name) { | |
_classAttributes.name = name; | |
return classConfig; | |
}; | |
this.extend = function() { | |
return (new AnimalFactory())._configure(); | |
}; | |
this._configure = function() { | |
return classConfig; | |
}; | |
this.instantiate = function() { | |
var instance = {}; | |
instance.speak = function() { | |
alert(_classAttributes.getVoice()); | |
}; | |
instance.walk = function() { | |
alert([ | |
_classAttributes.name, | |
"is walking with its", | |
_classAttributes.numLegs, | |
"legs" | |
].join(' ')); | |
}; | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment