Skip to content

Instantly share code, notes, and snippets.

@donabrams
Last active April 20, 2018 18:28
Show Gist options
  • Save donabrams/d91587c77dfb83f74270fac991f2cb76 to your computer and use it in GitHub Desktop.
Save donabrams/d91587c77dfb83f74270fac991f2cb76 to your computer and use it in GitHub Desktop.
Deep Read Only
const a = {
foo: "bar",
beep: {
boop: 1,
},
baz: [
true,
]
};
const deepReadOnly = (a) => new Proxy(a, {
get: function(target, prop, receiver) {
const a = Reflect.get(...arguments);
return typeof a === "object" ? deepReadOnly(a) : a;
},
set: () => {}
});
const b = deepReadOnly(a);
console.log(b.beep.boop);
b.beep.boop = 3;
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment