Last active
August 29, 2020 06:52
-
-
Save beetcb/aa0de797e3fbe3322eea5861081813a0 to your computer and use it in GitHub Desktop.
clone a object's flags
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
// using spread expression | |
let [one, two, three] = [...'12', { ...'abc' }] | |
const obj = { one, two, three } | |
function realDeepClone(obj, key, clone) { | |
if (!key) { | |
clone = Object.defineProperties({}, Object.getOwnPropertyDescriptors(obj)) | |
} else { | |
clone[key] = Object.defineProperties( | |
{}, | |
Object.getOwnPropertyDescriptors(obj) | |
) | |
} | |
Object.keys(obj).forEach(key => { | |
if (typeof obj[key] === 'object') return deepClone(obj[key], key, clone) | |
}) | |
return clone | |
} | |
let clone = realDeepClone(obj) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment