Skip to content

Instantly share code, notes, and snippets.

@bnielsen1965
Created November 3, 2016 21:48
Show Gist options
  • Save bnielsen1965/e989267112e21917ed8754ada8832cf6 to your computer and use it in GitHub Desktop.
Save bnielsen1965/e989267112e21917ed8754ada8832cf6 to your computer and use it in GitHub Desktop.
Javascript extend / merge for objects.
/**
* 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