3

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?

5
  • 1
    One thing you should be aware is that Object.freeze is shallow Commented Jun 11, 2017 at 5: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? Commented Jun 11, 2017 at 5:10
  • The example just demonstrates the problem. The actual code memoizes get to avoid creating multiple proxies for the same property Commented Jun 11, 2017 at 7:41
  • Can't you just clone the a object and use it as the target for proxy? Commented Jun 19, 2017 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. Commented Jun 23, 2017 at 11:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.