Skip to content

Instantly share code, notes, and snippets.

@kaimallea
Last active September 28, 2015 11:48
Show Gist options
  • Select an option

  • Save kaimallea/1434221 to your computer and use it in GitHub Desktop.

Select an option

Save kaimallea/1434221 to your computer and use it in GitHub Desktop.
Deep copy (recursively) properties from one object into another
/*
* Extend an object
*
* Deep copies (recursively) properties from source into target
*/
function extend (target, source) {
var prop;
for (prop in source) {
if (Object.prototype.hasOwnProperty.call(source, prop)) {
if (typeof source[prop] === 'object') {
if (source[prop] instanceof Array) {
target[prop] = source[prop].slice(0);
} else {
target[prop] = {};
extend(target[prop], source[prop]);
}
} else {
target[prop] = source[prop];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment