Problem: Prevent Accidental Mutation of Read-Only Settings
Problem statement
Your shared configuration object, config, is being mutated by multiple modules, which sometimes overwrites critical values, such as API URLs or version tags. This leads to unpredictable bugs in production.
You need a safeguard that prevents any property from being modified once loaded. The original configuration should remain immutable throughout the application’s lifetime.
Goal
Create a Proxy that blocks any write attempts and throws an error message such as "Cannot modify read-only settings" whenever code tries to change a property.
Constraints
You must use the
settrap.Do not freeze or clone the object. The restriction should come purely from Proxy behavior.
The Proxy must preserve normal read access to all properties.
Writing to a key should not change the original object.
Sample output
The examples below illustrate what the output should look like:
console.log(readOnlyConfig.appName);/* Expected output:NodeMaster*/try {readOnlyConfig.apiUrl = 'https://evil.com';} catch (err) {console.error(err.message);}/* Expected console error:Cannot modify read-only settings*/console.log(readOnlyConfig.apiUrl);/* Expected output:https://api.nodeapp.dev*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Prevent Accidental Mutation of Read-Only Settings
Problem statement
Your shared configuration object, config, is being mutated by multiple modules, which sometimes overwrites critical values, such as API URLs or version tags. This leads to unpredictable bugs in production.
You need a safeguard that prevents any property from being modified once loaded. The original configuration should remain immutable throughout the application’s lifetime.
Goal
Create a Proxy that blocks any write attempts and throws an error message such as "Cannot modify read-only settings" whenever code tries to change a property.
Constraints
You must use the
settrap.Do not freeze or clone the object. The restriction should come purely from Proxy behavior.
The Proxy must preserve normal read access to all properties.
Writing to a key should not change the original object.
Sample output
The examples below illustrate what the output should look like:
console.log(readOnlyConfig.appName);/* Expected output:NodeMaster*/try {readOnlyConfig.apiUrl = 'https://evil.com';} catch (err) {console.error(err.message);}/* Expected console error:Cannot modify read-only settings*/console.log(readOnlyConfig.apiUrl);/* Expected output:https://api.nodeapp.dev*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.