Last active
August 2, 2021 09:44
-
-
Save geraldyeo/73b179b46043a299d3e06b16587228b8 to your computer and use it in GitHub Desktop.
Object deep freeze
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
// https://blog.klipse.tech/javascript/2021/02/26/structural-sharing-in-javascript.html | |
function deepFreeze(object) { | |
const propNames = Object.getOwnPropertyNames(object); | |
// Freeze properties before freezing self | |
for (const name of propNames) { | |
const value = object[name]; | |
if (value && typeof value === "object") { | |
deepFreeze(value); | |
} | |
} | |
return Object.freeze(object); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment