Understanding of JavaScript Object Functions

Understanding of JavaScript Object Functions

JavaScript : Understanding of JavaScript Object.seal(), Object.freeze(), and Object.preventExtensions() functions

Understanding of JavaScript

JavaScript Object : Understanding of JavaScript is the ability to handle and modify objects makes JavaScript one of the most flexible and potent programming languages available. Object.seal(), Object.freeze(), and Object.preventExtensions() are three key JavaScript methods for controlling the mutability of objects. These techniques are essential for maintaining the integrity and immutability of objects, giving developers the resources they need to preserve data security and consistency. We will thoroughly examine each of these techniques and comprehend their applications in this post.

JavaScript Object.seal()

It is possible to prevent the addition or deletion of properties from an object by using the Object.seal() method. Additionally, it renders all currently existing properties non-configurable, preventing modification or deletion. The current properties’ values can still be changed, though.

const myObject = {
  name: 'Jahul',
  age: 20
};

Object.seal(myObject);

// Adding a new property is not allowed in object
myObject.country = 'India'; // Error

// Deleting properties is not allowed form the Object
delete myObject.age; // Error

// Modifying existing properties is allowed
myObject.name = 'Hasan';

Use cases for Object.seal() include:

  1. when you want to let property value change but prevent the addition or deletion of properties.
  2. Ensuring that certain properties are constant and unmodified during the course of an object’s life.

Using JavaScript Object.freeze()

An extension of the idea of immutability is provided by the Object.freeze() function. An object becomes fully unchangeable when it is frozen. This implies that you cannot only add or remove attributes but also change the values of already existing properties.

const myObject = {
  name: 'Jahul',
  age: 25
};

Object.freeze(myObject);

// Adding a new property is not allowed in object
myObject.country = 'India'; // Error

// Deleting properties is not allowed
delete myObject.age; // Error

// Modifying existing properties is not allowed
myObject.name = 'Hasan'; // Error

Use cases forĀ Object.freeze():

  • When you need to create objects with constant, unchanging data.
  • Ensuring that data remains consistent and cannot be unintentionally modified.

Object.preventExtensions()

The addition of new properties to an object can be stopped using the Object.preventExtensions() function. It prevents the object from being expanded with new properties, but it has no effect on the configurability or writeability of existing properties like Object.seal().

 

const myObject = {
  name: 'Jahul',
  age: 28
};

Object.preventExtensions(myObject);

// Adding a new property is not allowed in object
myObject.country = 'India'; // Error

// Deleting properties is allowed of project
delete myObject.age;

// Modifying existing properties is allowed for the object
myObject.name = 'Hasan';

Use cases forĀ Object.preventExtensions():

  • When you want to prevent new properties from being accidentally added to an object while still allowing the modification and deletion of existing properties.
  • Ensuring that an object has a fixed set of properties without any extensions.

Conclusion

According to their individual needs, developers can restrict the mutability of objects in JavaScript using the Object.seal(), Object.freeze(), and Object.preventExtensions() methods. By correctly implementing these techniques, you may improve the security and stability of your code, lower the possibility of unintended data changes, and make sure that your objects perform as expected for the duration of their useful lives. To understand JavaScript’s object manipulation capabilities, you must be aware of when to use each method.

 

JavaScript Game Development

Tools for Developer
Web Development Framework Angular

Related Posts

This Post Has 3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.