Horje
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 }



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript History, Versions JavaScript History, Versions
Difference Between Async & Defer Attributes in HTML Difference Between Async & Defer Attributes in HTML
TypeScript Exercises, Practice Questions and Solutions TypeScript Exercises, Practice Questions and Solutions
Localhost 3000 Localhost 3000
How to Create & Use Classes in JavaScript ? How to Create & Use Classes in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13