Skip to content

Instantly share code, notes, and snippets.

@TexRx
Last active August 29, 2015 14:22
Show Gist options
  • Save TexRx/04386dae2861aff9ae03 to your computer and use it in GitHub Desktop.
Save TexRx/04386dae2861aff9ae03 to your computer and use it in GitHub Desktop.
Recursive Object Extend
var extend = function(output) {
output = output || {};
// no args so just return output
if ( arguments.length < 2) return output;
// loop over arguments and recurse if prop is an object
for (var i = 1, len = arguments.length; i < len; i++) {
var obj = arguments[i];
if (!obj)
continue;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object')
extend(output[key], obj[key]);
else
output[key] = obj[key];
}
}
}
return output;
};
extend({}, objA, objB);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment