Make an Object Deeply Read-Only
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write readOnly(target), for a target made of plain objects and arrays: return a read-only view of it built with a Proxy. Reading through the view works as usual, and any object or array you reach through it is a read-only view too. Setting a property, deleting one, or defining one with Object.defineProperty, on the view or anything reached through it, throws a TypeError, and array methods that change the array, such as push, throw too. It's a view, not a frozen copy: the original target isn't frozen, code holding it can still change it, and those changes show through the view.
Your code must pass
- Reads values as usual
- Throws a TypeError on writes
- Throws a TypeError on deletes
- Protects nested objects and arrays
- Leaves the original changeable and shows its changes