Skip to content

Instantly share code, notes, and snippets.

@solarsailer
Created January 4, 2012 13:23
Show Gist options
  • Save solarsailer/1560019 to your computer and use it in GitHub Desktop.
Save solarsailer/1560019 to your computer and use it in GitHub Desktop.
Merge options param to a defaults object (first draft)
/**
* Optional params pattern.
* Merge a user defined object with a default one.
*/
setOptions: function(defaults, options) {
/* Force the obj param to be an object literal */
if (typeof defaults !== 'object') defaults = {};
if (typeof options !== 'object') options = {};
/*
* For every property of the `defaults` object
* we check if the `options` object has defined
* the same property, and if it's the case
* we merge the newer property to the `defaults`
* object.
*/
for (var prop in defaults) {
if (defaults.hasOwnProperty(prop)) {
/* Actual merge */
if (options[prop]) defaults[prop] = options[prop];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment