Skip to content

Instantly share code, notes, and snippets.

@inancgumus
Last active March 27, 2016 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inancgumus/e1353d08e43a528ab60c to your computer and use it in GitHub Desktop.
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.
// 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