What is Object.seal() method in JavaScript ?
The Object.seal()
method in JavaScript is used to seal an object, preventing new properties from being added to it and existing properties from being removed. However, the values of existing properties can still be changed.
Syntax:
Object.seal(obj)
Example: Here, Object.seal()
is used to seal the obj
object. Attempts to add or remove properties have no effect, but modifying existing properties (prop
) is allowed. The object’s structure remains unchanged, demonstrating the behavior enforced by Object.seal()
.
Javascript
const obj = { prop: 42 }; Object.seal(obj); // Attempting to add a new property obj.newProp = 10; // Attempting to delete an existing property delete obj.prop; // Modifying an existing property obj.prop = 33; console.log(obj); // Output: { prop: 33 } |
Output
{ prop: 33 }
Contact Us