Last active
March 27, 2016 13:11
-
-
Save inancgumus/e1353d08e43a528ab60c to your computer and use it in GitHub Desktop.
this pattern has usually been used in Java for testing. but, in javascript, it's very easy to do without the boilerplate code hassle.
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
// general helper to create the builder pattern automatically | |
var make = function(klass) { | |
return { with: function(extendWithThis) { return Object.assign(klass, extendWithThis) } } | |
} | |
// let's make a spaghetti | |
var Pasta = { ingredients: [] } | |
var spaghetti = make(Pasta).with({ | |
ingredients: [ 'mashroom', 'onion' ] | |
}) | |
// let's make another spaghetti with blognose sauce | |
var spaghettiBolognese = make(spaghetti).with({ | |
sauce: [ 'bolognese' ] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment