Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created April 24, 2017 21:48
Show Gist options
  • Save slugbyte/e2e23bf5f310700ea5bea3b7dcfc5ba3 to your computer and use it in GitHub Desktop.
Save slugbyte/e2e23bf5f310700ea5bea3b7dcfc5ba3 to your computer and use it in GitHub Desktop.
// extendy is a higher-order frunction that wraps to constructors
// and allows b to "inherit" from a
extendy = (a) => ( b) => (opts={}) => Object.assign({}, a(opts.super || {}), b(opts))
// create a cow "constructor"
cow = (opts={}) => ({utters: opts.utters || 4, milk: () => "got milk?"})
// create a stawbery cow "constructor" that extends cow
strawbery = extendy(cow)((opts={}) => ({flavor: 'strawbery', expires: opts.expires || 'tomorrow'}))
// with all defaults
strawbery()
// overwrite strawbery prop
strawbery({expires: 'lulwat'}) // override strawbery prop
// opverwrite super prop
pinkcow = strawbery({expires: 'lulwat', super: {utters: 8}}) // override super prop :p
// pinkcow is an "strawbery" but has "cow"'s method 'milk'
pinkcow.milk()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment