Skip to content

Instantly share code, notes, and snippets.

@gadzhimari
Created February 18, 2018 08:00
Show Gist options
  • Save gadzhimari/afb19aa3bed1c76d87a07fffbf30e2ec to your computer and use it in GitHub Desktop.
Save gadzhimari/afb19aa3bed1c76d87a07fffbf30e2ec to your computer and use it in GitHub Desktop.
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