Skip to content

Instantly share code, notes, and snippets.

@benaston
Last active August 29, 2015 14:24
Show Gist options
  • Save benaston/a3dc94b7cd1d00f94fdd to your computer and use it in GitHub Desktop.
Save benaston/a3dc94b7cd1d00f94fdd to your computer and use it in GitHub Desktop.
var need = require('niid').need;
var mix = require('mixx').mix;
var defaultOptions = Object.freeze({ foo: undefined,
bar: undefined,
bam: null,
baz: null });
function MyThing(options) {
need(options, 'foo', 'bar'); // `foo` and `bar` are required.
options = mix({}, defaultOptions, options);
mix(this, options);
}
// -- or, in fewer lines --
function MyThing(options) {
need(options = mix({}, defaultOptions, options), 'foo', 'bar'); // `foo` and `bar` are required.
mix(this, options);
}
/**
* This results in an object with own-properties
* `foo`, `bar`, `bam` and `baz`, with the supplied
* values overriding the defaults.
*/
var myThing = new MyThing({ foo: 'foo', bar: 'bar' });
@benaston
Copy link
Author

benaston commented Jul 3, 2015

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