Skip to content

Instantly share code, notes, and snippets.

@Ado77
Forked from ericelliott/defaults-overrides.md
Created September 7, 2018 14:07
Show Gist options
  • Select an option

  • Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.

Select an option

Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.
ES6 defaults / overrides pattern
// 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