Codementor Events

Prevent modification of object in JavaScript

Published May 01, 2019
Prevent modification of object in JavaScript

Go to the profile of Gaurav Singh

Gaurav Singh

ECMAScript 5 introduce several methods to prevent modification of object which lock down object to ensure that no one, accidentally or otherwise, change functionality of Object.

There are three levels of preventing modification:

1: Prevent extensions :

No new properties or methods can be added to the object, but one can change the existing properties and method.

For example:

var employee = {
  name: "Nishant"
};

// lock the object 
Object.preventExtensions(employee);

// Now try to change the employee object property name
employee.name = "John"; // work fine

//Now try to add some new property to the object
employee.age = 24; // fails silently unless it's inside the strict mode

2: Seal :

It is same as prevent extension, in addition to this also prevent existing properties and methods from being deleted.

To seal an object, we use Object.seal() method. you can check whether an object is sealed or not using Object.isSealed();

var employee = {
  name: "Nishant"
};

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee)); // true

delete employee.name // fails silently unless it's in strict mode

// Trying to add new property will give an error
employee.age = 30; // fails silently unless in strict mode

when an object is sealed, its existing properties and methods can’t be removed. Sealed object are also non-extensible.

3: Freeze :

Same as seal, In addition to this prevent existing properties methods from being modified (All properties and methods are read only).

To freeze an object, use Object.freeze() method. We can also determine whether an object is frozen using Object.isFrozen();

var employee = {
  name: "Nishant"
};

//Freeze the object
Object.freeze(employee);

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee)); // true
console.log(Object.isFrozen(employee)); // true

employee.name = "xyz"; // fails silently unless in strict mode
employee.age = 30; // fails silently unless in strict mode
delete employee.name // fails silently unless it's in strict mode

Frozen objects are considered both non-extensible and sealed.

Recommended:

If you are decided to prevent modification, sealed, freeze the object then use in strict mode so that you can catch the error.

For example:

"use strict";

var employee = {
  name: "Nishant"
};

//Freeze the object
Object.freeze(employee);

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee)); // true
console.log(Object.isFrozen(employee)); // true

employee.name = "xyz"; // fails silently unless in strict mode
employee.age = 30; // fails silently unless in strict mode
delete employee.name; // fails silently unless it's in strict mode
Discover and read more posts from Gaurav Singh
get started
post commentsBe the first to share your opinion
Jonathan Eunice
5 years ago

Good start on immutability. One caution: Sealing and freezing are shallow operations in JavaScript. They prevent mutation of the sealed or frozen object per se, but not of any sub-objects. Sub-objects and contained objects (e.g. elements of arrays) are not sealed/frozen at the same time the parent object is, so remain completely mutable. If you want to utterly seal/freeze an object and all of its constituent parts, you’ll need to recursively walk sub-objects to individually seal/freeze them as well.

Gaurav Singh
5 years ago

Sure. Thanks for highlighting it. :)

Show more replies