Skip to content

Instantly share code, notes, and snippets.

@ositowang
Created April 1, 2019 01:11
Show Gist options
  • Save ositowang/d1a51c01948f98724e6eac64979ba321 to your computer and use it in GitHub Desktop.
Save ositowang/d1a51c01948f98724e6eac64979ba321 to your computer and use it in GitHub Desktop.
Maybe good enough for interview Simpliest Version of deepClone.
/**
* Maybe good enough for interview Simpliest Version of deepClone.
* Problems:
* 1. the type check of object is not strict
* 2. did not cover array
* 3. did not cover null
* @param {Object} sourceObj
* @returns
*/
var deepCloneSimple = (sourceObj) => {
let result = {};
for (let key in sourceObj) {
if (Object.prototype.hasOwnProperty.call(sourceObj, key)) {
if (typeof sourceObj[key] === 'object') {
result[key] = deepCloneSimple(sourceObj[key]);
} else {
result[key] = sourceObj[key];
}
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment