Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created September 17, 2012 17:57
Show Gist options
  • Save wilmoore/3738779 to your computer and use it in GitHub Desktop.
Save wilmoore/3738779 to your computer and use it in GitHub Desktop.
UnderscoreJS-inspired `merge` taking advantage of ES5 where possible
/**
* UnderscoreJS inspired `merge`
* UnderscoreJS calls it `extend` (overloaded in JS); `merge` feels more intention revealing
* This is naive as it always overwrites if the target property exists.
*
* @see https://github.com/documentcloud/underscore/blob/master/underscore.js#L727
* @param {Object} target
* @return {Object} target
*/
function merge(target) {
[].slice.call(arguments, 1).forEach(function(source) {
Object.keys(source).forEach(function(key){
target[key] = source[key];
});
});
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment