Created
March 26, 2015 23:03
-
-
Save potench/fa907b61d82eb7e3091f to your computer and use it in GitHub Desktop.
deepExtend function in es6
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
let isObject = (obj) => { | |
let type = typeof obj; | |
return type === "function" || type === "object" && !!obj; | |
}; | |
let deepExtend = (obj, ...args) => { | |
if (!isObject(obj)) { | |
return obj; | |
} | |
let source, | |
prop, | |
i = 0, | |
length = args.length; | |
for (i; i < length; i++) { | |
source = args[i]; | |
for (prop in source) { | |
if (source.hasOwnProperty(prop)) { | |
if (typeof obj[prop] === "object" && obj[prop] !== null) { // deep copy | |
deepExtend(obj[prop], source[prop]); | |
} else { | |
obj[prop] = source[prop]; | |
} | |
} | |
} | |
} | |
return obj; | |
}; | |
export default deepExtend; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment