Created
January 4, 2012 13:23
-
-
Save solarsailer/1560019 to your computer and use it in GitHub Desktop.
Merge options param to a defaults object (first draft)
This file contains 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
/** | |
* 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