2

I'm trying to deeply proxy the properties of a frozen object:

const a = Object.freeze([{ prop: 1 }])

const proxy = new Proxy(a, {
    get(target, property) {
        return new Proxy(target[property], {});
    }
})

console.log(proxy[0])

This produces a type error:

TypeError: 'get' on proxy: property '0' is a read-only and non-configurable data property on the proxy target but the proxy did not return

and I see that get proxies have the following restriction:

The value reported for a property must be the same as the value of the corresponding target object property if the target object property is a non-writable, non-configurable own data property. - es6 spec

Is there any way to have nested proxies on a frozen object?

Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
  • One thing you should be aware is that Object.freeze is shallow – nem035 Jun 11 '17 at 05:06
  • Also, this code creates a proxy on each `get` call? Is that intentional? Or do you just want to have a proxy object to a nested object? – nem035 Jun 11 '17 at 05:10
  • The example just demonstrates the problem. The actual code memoizes `get` to avoid creating multiple proxies for the same property – Matt Bierner Jun 11 '17 at 07:41
  • Can't you just clone the `a` object and use it as the target for proxy? – Michał Perłakowski Jun 19 '17 at 18:54
  • I'd say don't freeze your objects, since the effect is shallow anyway. Rather than enforcing immutability, treat it as a guideline/policy in Javascript. When you use pure functions and combinators like lenses and appropriate data structures (recursive lists), mutations should hardly occur. Btw, if you have records in mind, `Object.prototype.seal` is a good tradeoff. –  Jun 23 '17 at 11:15

0 Answers0