Preventing modification of Objects

There are many ways to lock an object according to your needs.

  1. Object.freeze()
  2. Object.seal()
  3. Object.preventExtensions()

Object.freeze()

To safeguard JavaScript objects from modifications, a recommended approach involves employing the Object.freeze() method. By applying this method, the addition of new properties to an object is prohibited, and any endeavor to modify or eliminate existing properties is thwarted. Such attempts are rendered ineffective, leading to either a discreet failure or the triggering of a TypeError exception. This outcome is particularly prevalent when operating within the confines of strict mode, although not exclusively so.

Syntax
Object.freeze(obj)
example
var student = { name: "John", age: 10 }; Object.freeze(student); student.grade = 2; // Throws an error in strict mode document.write(student.grade); //undefined document.write("<br/>"); delete student.age; //delete property document.write(student.age); document.write("<br/>"); student.name = "Doe"; document.write(student.name);
output
undefined 10 John

The process of object freezing proves valuable in the representation of logically immutable data structures, particularly when altering object properties could potentially result in adverse effects within your application. It's noteworthy that the application of Object.freeze(…) is limited to a shallow level. Consequently, object values within a frozen object remain susceptible to mutation.

Object.seal()

Object.seal extends the functionality of Object.preventExtensions by not only preventing the addition of arbitrary properties to objects but also by prohibiting the modification of attributes associated with existing properties. Furthermore, Object.seal disallows the deletion of properties altogether.

Syntax
Object.seal(obj)
example
var student = { name: "John", age: 10 }; Object.seal(student); student.grade = 2; //add new property document.write(student.grade); //undefined document.write("<br/>"); student.name = "Doe"; document.write(student.name); document.write("<br/>"); //Doe delete student.age; document.write(student.age); // 10 document.write("<br/>");
output
undefined Doe 10

Object.preventExtensions()

The utilization of Object.preventExtensions() involves setting the extensible attribute of the object 'o' to false, effectively preventing the addition of new properties to it. This alteration is irreversible, as once an object's extensibility has been revoked, it cannot be reconfigured to be extensible again.

example
var student = { name: "John", age: 10 }; Object.preventExtensions(student); document.write(Object.isExtensible(student)); document.write("<br/>"); student.grade = 2; //add new property document.write(student.grade); //undefined document.write("<br/>");
output
false undefined

Conclusion

Object modification can be prevented using methods like Object.freeze(), which makes objects effectively immutable by disallowing addition, removal, or alteration of properties. Object.preventExtensions() stops the addition of new properties while maintaining existing ones, while Object.seal() goes a step further by prohibiting both addition and modification of properties.