Created
February 18, 2018 08:00
-
-
Save gadzhimari/afb19aa3bed1c76d87a07fffbf30e2ec to your computer and use it in GitHub Desktop.
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
const deepFreeze = (obj) => { | |
// Retrieve the property names defined on obj | |
const propNames = Object.getOwnPropertyNames(obj); | |
// Freeze properties before freezing self | |
propNames.forEach(function(name) { | |
const prop = obj[name]; | |
// Freeze prop if it is an object | |
if (typeof prop == 'object' && prop !== null) { | |
deepFreeze(prop); | |
} | |
}); | |
// Freeze self (no-op if already frozen) | |
return Object.freeze(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment