How do you use the Object.seal() method in JavaScript?

In JavaScript, you can use the Object.seal() method to prevent the addition or removal of properties from an object.

When an object is sealed, its existing properties cannot be deleted, and new properties cannot be added. However, the values of the existing properties can be modified.

The Object.seal() method takes an object as its argument and returns the same object that was passed in.

Let’s take a look at how to use Object.seal() in JavaScript.

Sealing an object

const person = {
  name: "John",
  age: 30,
};

Object.seal(person);

// Attempting to add a new property
person.email = "john@example.com"; // Fails silently

// Attempting to delete an existing property
delete person.age; // Fails silently

// Modifying an existing property
person.name = "Jane";

console.log(person); // { name: 'Jane', age: 30 }

In the above example, we define an object person with two properties: name and age. We then call Object.seal() on the person object, which prevents any new properties from being added or existing properties from being deleted.

Next, we attempt to add a new property to the person object using the dot notation. This operation fails silently, meaning that no error is thrown, but the email property is not added to the object.

We also attempt to delete the age property using the delete keyword. This operation also fails silently, and the age property remains in the object.

Finally, we modify the name property using the dot notation. This operation is allowed because we are modifying an existing property, not adding a new one.

Checking if an object is sealed

const person = {
  name: "John",
  age: 30,
};

Object.seal(person);

console.log(Object.isSealed(person)); // true

In this example, we define an object person and call Object.seal() on it to seal the object. We then use the Object.isSealed() method to check if the object is sealed. The Object.isSealed() method returns a boolean value indicating whether an object is sealed or not.

Sealing an object and checking property descriptors:

const person = {
  name: "John",
  age: 30,
};

Object.seal(person);

const namePropertyDescriptor = Object.getOwnPropertyDescriptor(person, "name");
console.log(namePropertyDescriptor); // { value: 'John', writable: true, enumerable: true, configurable: false }

const agePropertyDescriptor = Object.getOwnPropertyDescriptor(person, "age");
console.log(agePropertyDescriptor); // { value: 30, writable: true, enumerable: true, configurable: false }

person.name = "Jane";
namePropertyDescriptor.value = "Joe";

console.log(person); // { name: 'Jane', age: 30 }
console.log(namePropertyDescriptor); // { value: 'Joe', writable: true, enumerable: true, configurable: false }

In this example, we define an object person with two properties: name and age. We then call Object.seal() on the person object to seal it.

Next, we use the Object.getOwnPropertyDescriptor() method to retrieve the property descriptors for the name and age properties. The property descriptors tell us whether a property is writable, enumerable, and configurable.

We then attempt to modify the name property using both the dot notation and the namePropertyDescriptor. Both of these modifications are allowed because the writable attribute of the name property is set to true.

In the previous example, we modified the name property even though the person object is sealed. This is because the writable attribute of the name property is set to true.

To prevent modifications to the name property, we can use the Object.defineProperty() method to change the writable attribute to false.

Sealing an object and changing property descriptors:

const person = {
  name: "John",
  age: 30,
};

Object.seal(person);

const namePropertyDescriptor = Object.getOwnPropertyDescriptor(person, "name");
console.log(namePropertyDescriptor); // { value: 'John', writable: true, enumerable: true, configurable: false }

Object.defineProperty(person, "name", { writable: false });

person.name = "Jane";

console.log(person); // { name: 'John', age: 30 }

In this example, we define an object person with two properties: name and age. We then call Object.seal() on the person object to seal it.

Next, we use the Object.getOwnPropertyDescriptor() method to retrieve the property descriptor for the name property. We see that the writable attribute is set to true.

We then use the Object.defineProperty() method to change the writable attribute of the name property to false. This prevents the name property from being modified.

Finally, we attempt to modify the name property using the dot notation. This operation fails silently, and the name property remains unchanged.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: