Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active October 18, 2018 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelo-ribeiro/6ddf565ba5bc81c36995e0ffd404a0cb to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/6ddf565ba5bc81c36995e0ffd404a0cb to your computer and use it in GitHub Desktop.
Polyfill js to extend objects and child objects
var defaults = {
firstName = 'John',
lastName = 'Doe'
};
var options = {
firstName = 'Jane'
};
extendDefaults(defaults, options);
console.log(options);
// {firstName = 'Jane', lastName = 'Doe'};
function extendDefaults (a, b) {
for ( var prop in a ) {
if ( typeof a[prop] === 'object' && b.hasOwnProperty(prop) ) {
extendDefaults(a[prop], b[prop]);
continue;
}
if ( !b.hasOwnProperty(prop) ) b[prop] = a[prop];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment