Skip to content

Instantly share code, notes, and snippets.

@corymartin
Last active August 29, 2015 14:27
Show Gist options
  • Save corymartin/ad560de3cf80204a2c14 to your computer and use it in GitHub Desktop.
Save corymartin/ad560de3cf80204a2c14 to your computer and use it in GitHub Desktop.
Mutates an object hash into an object of getters and setters where the setters - if used - will throw an error.
function immutabilly(props) {
Object.keys(props).forEach(key => {
const prop = props[key];
if (prop === Object(prop)) {
immutabilly(prop);
}
Object.defineProperty(props, key, {
enumerable: true,
get: () => prop,
set: () => {throw new Error(`Illegal assignment to immutable property '${key}'`);},
});
});
return props;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment