Last active
August 29, 2015 14:22
-
-
Save TexRx/04386dae2861aff9ae03 to your computer and use it in GitHub Desktop.
Recursive Object Extend
This file contains hidden or 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 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