Skip to content

Instantly share code, notes, and snippets.

@potench
Created March 26, 2015 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potench/fa907b61d82eb7e3091f to your computer and use it in GitHub Desktop.
Save potench/fa907b61d82eb7e3091f to your computer and use it in GitHub Desktop.
deepExtend function in es6
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