Last active
September 28, 2015 11:48
-
-
Save kaimallea/1434221 to your computer and use it in GitHub Desktop.
Deep copy (recursively) properties from one object into another
This file contains hidden or 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
| /* | |
| * 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