Object.seal( ) In JavaScript

Object and Object Constructors in JavaScript?
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.

  • Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
  • Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.

For instance, consider the following code:

filter_none

edit
close

play_arrow

link
brightness_4
code

function Automobile(color) {
  this.color=color;
}
  
var vehicle1 = new Automobile ("red");

chevron_right


The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”.



When new Automobile() is called, JavaScript does two things:

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.seal() Method
Among the Object constructor methods, there is a method Object.seal() which is used to seal an object. Sealing an object does not allow new properties to be added and marks all existing properties as non-configurable. Although values of present properties can be changed as long as they are writable.
The object to be sealed is passed as an argument and the method returns the object which has been sealed.

Difference between Object.freeze() Method and Object.seal() Method

If an object is frozen using the Object.freeze() method then its properties become immutable and no changes can be made in them whereas if an object is sealed using the Object.seal() method then the changes can be made `in the existing properties of the object.

Applications:

  • Object.seal() is used for sealing objects and arrays.
  • Object.seal() is used to make an object immutable.

Syntax:

Object.seal(obj)

Parameters Used:

  1. obj : It is the object which has to be sealed.

Return Value:
Object.sealed() returns the object that was passed to the function.

Examples of the above function are provided below.

Examples:


Input : const obj1 = { property1: 'initial_data'};
        const obj2 = Object.seal(obj1);
        obj2.property1 = 'new_data';
        console.log(obj2.property1);

Output : "new_data"

Explanation : In this example, the object “ob2” has been assigned properties of object “obj1” and it is been sealed so that new values cannot be added. The value of property 1 for obj2 has been updated since sealing an object allows existant properties to be changed.

Input : var obj = { prop: function() {}, name: 'adam' };
        console.log(obj);
        obj.name = 'billy';
        delete obj.prop;
        console.log(obj);
        var o = Object.seal(obj);
        delete obj.prop;
        console.log(obj);
        obj.name = 'chris';
        console.log(obj);

Output : Object { prop: function () {}, name: "adam" }
         Object { name: "billy" }
         Object { name: "billy" }
         Object { name: "chris" }

Explanation : In this example, the object “obj” has been assigned “prop: function” which has been later deleted since the object “obj wasn’t sealed. After that, a new object “o” has been assigned the sealed values of “obj” which prevented it from deletion but allowed updations in the existing properties.

Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating an object constructor and assigning values to it 
const obj1 = { property1: 'initial_data'};
  
// creating a second object which will seal the properties of the first object
const obj2 = Object.seal(obj1);
  
// Updating the properties of the frozen object
obj2.property1 = 'new_data';
  
// Displaying the properties of the  frozen object 
console.log(obj2.property1);
</script>

chevron_right


OUTPUT :

"new_data"

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating an object constructor and assigning values to it 
var obj = { prop: function() {}, name: 'adam' };
  
// Displaying the properties of the object created 
console.log(obj);
  
// Updating the properties of the object 
obj.name = 'billy';
delete obj.prop;
  
// Displaying the updated properties of the object 
console.log(obj);
  
// Sealing the object using object.seal() method
var o = Object.seal(obj);
  
// Updating the properties of the object 
delete obj.prop;
  
// Displaying the updated properties of the object 
console.log(obj);
  
// Updating the properties of the sealed object 
obj.name = 'chris';
  
// Displaying the properties of the  frozen object 
console.log(obj);
  
</script>

chevron_right


OUTPUT :

Object { prop: function () {}, name: "adam" }
Object { name: "billy" }
Object { name: "billy" }
Object { name: "chris" }

Exceptions :

  • It causes a TypeError if the argument passed is not an object .
  • Deleting or adding properties to a sealed object will fail or throw a TypeError.
  • Converting a data property to accessor or its vice versa will throw a TypeError.

Reference :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.