In JavaScript, there is a built-in
`seal()` method that belongs to the `Object` constructor. The `seal()` method is used to prevent new properties from being added to an
object and marks existing properties as non-configurable.
When you call `Object.seal(obj)`, it seals the specified `obj` object, which means you cannot add or remove properties from it. However, you can still modify the values of existing properties.
Here's an example to illustrate the usage of the `seal()` method:
const obj = {
prop1: 42,
prop2: 'Hello'
};
Object.seal(obj);
// Attempting to add a new property
obj.prop3 = true; // Ignored, property will not be added
// Attempting to delete an existing property
delete obj.prop1; // Ignored, property will not be deleted
// Modifying the value of existing properties is allowed
obj.prop1 = 100;
obj.prop2 = 'World';
console.log(obj); // { prop1: 100, prop2: 'World' }
In this example, the
`Object.seal(obj)` statement seals the `obj` object. The subsequent attempts to add or delete properties (`prop3` and `prop1`, respectively) are ignored. However, modifying the values of existing properties (`prop1` and `prop2`) is still allowed.
Please note that sealing an object using `Object.seal()` does not make the object's properties read-only. You can still change their values. It only prevents structural changes to the object itself, such as adding or removing properties or changing their configurability.