javascript / intermediate
Snippet
Preventing Property Additions with Object.seal()
Object.seal() locks an object's structure. It prevents the addition or deletion of properties but still allows you to modify the values of existing properties.
snippet.js
1
2
3
4
5
6
const config = { api: 'v1', timeout: 5000 };Object.seal(config);config.timeout = 3000; // Success: Values can be changedconfig.token = 'secret'; // Failure: No new properties alloweddelete config.api; // Failure: Properties cannot be removed
Breakdown
1
Object.seal(config);
Seals the object, preventing schema changes.
2
config.timeout = 3000;
Updating existing properties remains functional.