-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeepSeal.js
36 lines (29 loc) · 817 Bytes
/
deepSeal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function deepSeal(obj) {
// Seal the current object
Object.seal(obj);
// Recursively seal all properties that are objects
for (const key in obj) {
if (typeof obj[key] === "object" && obj[key] !== null) {
deepSeal(obj[key]);
}
}
return obj;
}
// Example usage
const config = {
api: {
url: "https://api.example.com",
timeout: 5000,
},
settings: {
theme: "dark",
notifications: true,
},
};
deepSeal(config);
// Trying to add a new property (will fail)
config.api.newProperty = true; // ❌ Error: Cannot add property
// Modifying an existing property (allowed)
config.api.timeout = 7000; // ✅ Allowed
// Trying to delete a property (will fail)
delete config.settings.theme; // ❌ Error: Cannot delete property