Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from ericf/gist:3751340
Created September 19, 2012 18:43
Show Gist options
  • Save rwaldron/3751415 to your computer and use it in GitHub Desktop.
Save rwaldron/3751415 to your computer and use it in GitHub Desktop.
ES6 Object.assign( target, source ) as a native options object merging pattern.
let defaults = { file: null };
function foo(options) {
// Merging defaults and options objects in ES6
options = [ defaults, options ].reduce(Object.assign, {});
}
foo({ file: "happy.md" });
/*
Object.assign( target, source )
When passed as the callback to reduce, the params are bound as:
function( prev, value ) {}
*/

Object.assign( target, source )

Object.assign( target, source )

Rough spec expectations:

  • Only enumerable own properties of source
  • Invoke [[Get]] on property list derived from source, for each property in list [[Put]] on target
  • private names are not copied
  • unique names are copied
  • super mechanism (rebind super)… AWB To determine needs
  • Returns modified "target"
@killroy42
Copy link

killroy42 commented Jul 24, 2016

Wouldn't this make more sense: (I might be late to this discussion)
constructor({opt1, opt2, opt3} = {opt1: def1, opt2: def2, opt3: def3}) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment