Last active
April 26, 2020 13:02
-
-
Save riffrain/14a407dc754cbeb2f5d9bb835bf1df1a to your computer and use it in GitHub Desktop.
Deep freeze Object
This file contains hidden or 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
function deepFreeze(object) { | |
const clone = JSON.parse(JSON.stringify(object)); | |
for (const name of Object.getOwnPropertyNames(clone)) { | |
if (typeof clone[name] === "object" && clone[name] !== null) { | |
clone[name] = deepFreeze(clone[name]); | |
} | |
} | |
return Object.freeze(clone); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment