Last active
October 18, 2018 18:21
-
-
Save marcelo-ribeiro/6ddf565ba5bc81c36995e0ffd404a0cb to your computer and use it in GitHub Desktop.
Polyfill js to extend objects and child objects
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
var defaults = { | |
firstName = 'John', | |
lastName = 'Doe' | |
}; | |
var options = { | |
firstName = 'Jane' | |
}; | |
extendDefaults(defaults, options); | |
console.log(options); | |
// {firstName = 'Jane', lastName = 'Doe'}; |
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
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