Created
April 1, 2019 01:11
-
-
Save ositowang/d1a51c01948f98724e6eac64979ba321 to your computer and use it in GitHub Desktop.
Maybe good enough for interview Simpliest Version of deepClone.
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
/** | |
* 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