

Secure Object Proxy
Write a function createSecureObject(target, allowedKeys) that returns a Proxy wrapping target and traps property read and write operations:
get(target, prop): Ifpropis NOT inallowedKeys, throw anError('Access Denied'). Otherwise, returntarget[prop].set(target, prop, value): Ifpropis NOT inallowedKeys, throw anError('Write Denied'). Otherwise, settarget[prop] = valueand returntrue.
Also write a helper function test_secure_proxy(propToRead, propToWrite, valToWrite) that:
- Creates a target object
{ name: 'Nebula', type: 'gas' }. - Wraps it using
createSecureObjectandallowedKeys = ['name', 'type', 'density']. - Tries to read
propToRead. If it throws an error, return'read error'. - Tries to write
valToWritetopropToWrite. If it throws an error, return'write error'. - Returns the value of
propToWriteon the proxy.
Examples
test_secure_proxy("name", "density", 95)
→ 95
test_secure_proxy("secret", "density", 95)
→ "read error"
solution.js
JAVASCRIPT
Saved as you type
Tests
0 of 3 passing- •valid read and writetest_secure_proxy("name", "density", 95)expected 95
- •invalid read accesstest_secure_proxy("secret", "density", 95)expected "read error"
- •invalid write accesstest_secure_proxy("name", "secret", 95)expected "write error"
On the line
+70 XP
Pass all 3 tests to claim it.
