Created
November 3, 2016 21:48
-
-
Save bnielsen1965/e989267112e21917ed8754ada8832cf6 to your computer and use it in GitHub Desktop.
Javascript extend / merge for objects.
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
/** | |
* Extend an objects properties by merging with additional object arguments. | |
* The source objects should be in a comma separated list after the target | |
* parameter. | |
* @param {object} target - The target object for the merge. | |
* @return {object} The final object from target merged with additional objects in the argument list. | |
*/ | |
self._extend = function _extend(target) { | |
var sources = [].slice.call(arguments, 1); | |
sources.forEach(function (source) { | |
for (var prop in source) { | |
if (typeof source[prop] === 'object' && source[prop] !== null && !Array.isArray(source[prop])) { | |
if (typeof target[prop] === 'undefined') { | |
target[prop] = {}; | |
} | |
target[prop] = self._extend(target[prop], source[prop]); | |
} | |
else { | |
target[prop] = source[prop]; | |
} | |
} | |
}); | |
return target; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment