-
-
Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.
ES6 defaults / overrides pattern
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
| // The defaults / overrides pattern in ES6 | |
| let defaults = { | |
| bar: 'no', | |
| baz: 'works!' | |
| }; | |
| function foo ({ bar, baz } = defaults) { | |
| return(`${bar}, ${baz}`); | |
| } | |
| console.log(foo({ | |
| bar: 'yay', | |
| baz: 'works!'})); | |
| // Equivalent to ES5: | |
| // This bit needs to be polyfilled, | |
| // Or use $.extend(), _.extend(), | |
| // lodash/object/assign aka _.assign() | |
| // or equivalent. | |
| var assign = Object.assign; | |
| var defaults2 = { | |
| bar: 'no', | |
| baz: 'works!' | |
| }; | |
| function foo2 (options) { | |
| var settings = Object.assign({}, defaults2, options); | |
| } | |
| console.log(foo2({ | |
| bar: 'yay', | |
| baz: 'works!'})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment