Skip to content

Instantly share code, notes, and snippets.

@jiggliemon
Created November 9, 2011 08:27
Show Gist options
  • Save jiggliemon/1350840 to your computer and use it in GitHub Desktop.
Save jiggliemon/1350840 to your computer and use it in GitHub Desktop.
Merge in values from other objects that share the master objects properties.
if(typeof Object.merge !== 'function')
Object.merge = function() {
var base = arguments[0]
args = Array.prototype.slice.call(arguments,1),
count = args.length,
keys = Object.keys(base),
mergeIn = undefined,key = undefined,i = 0;
mergeIn = function(obj){
var keys = Object.keys(this),
key;
for(key in obj){
if(obj.hasOwnProperty(key) && keys.indexOf(key) >= 0){
this[key] = obj[key];
}
}
}
for (;i < count; i++){
mergeIn.call(base,args[i]);
}
return base;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment