Created
April 24, 2017 21:48
-
-
Save slugbyte/e2e23bf5f310700ea5bea3b7dcfc5ba3 to your computer and use it in GitHub Desktop.
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
// 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