Last active
August 29, 2015 14:13
-
-
Save iainmcampbell/b10394c59c862fb4e331 to your computer and use it in GitHub Desktop.
JS Object Merge
This file contains 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
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; | |
} |
This file contains 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
// 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