Skip to content

Instantly share code, notes, and snippets.

@iainmcampbell
Last active August 29, 2015 14:13
Show Gist options
  • Save iainmcampbell/b10394c59c862fb4e331 to your computer and use it in GitHub Desktop.
Save iainmcampbell/b10394c59c862fb4e331 to your computer and use it in GitHub Desktop.
JS Object Merge
function extend(){
var output = {},
args = arguments,
l = args.length;
for ( var i = 0; i < l; i++ )
for ( var key in args[i] )
if ( args[i].hasOwnProperty(key) )
output[key] = args[i][key];
return output;
}
// Default object
var defaults = {
asdf: 1,
jkl: 200
}
// opts object: extends and overwrites defaults
var opts = {
asdf: 2,
foo: 10
}
var options = extend(defaults, opts)
/*
returns: {
asdf: 2,
jkl: 200,
foo: 10
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment