An Overview of Javascript Object.seal() Method for Beginners

Javascript Object.seal() Method ensures no new properties and methods to be added to the sealed object. But the existing object properties value can updated.

Syntax

Object.seal(object);

The Object.seal() Method takes object as parameter and returns new sealed object.

var fruit = {"name" : "Apple", "color" : "red"};
Object.seal(fruit);

//adding new property `weight`
fruit.weight = "56.02 grams"; 

//adding new function `get_name`
fruit.get_name = function(){
	return this.name.toUpperCase();
};

console.log(fruit); //displaying object 
console.log(fruit.get_name());  //calling object method

//Console Output
{name: "Apple", color: "red"}
Uncaught TypeError: fruit.get_name is not a function
Exception on calling the get_name() method on object fruit
Exception on calling the get_name() method on object fruit

In above example we have fruit object which has name and color property. After sealing that object we added new property weight and new method get_name() to fruit object. But throws exception on adding new property or method.

Table of Contents

Difference between Javascript Object.freeze() and Object.seal() method

It may look as both Object.freeze() and Object.seal() method similar but they are very different from each other. Where `Object.freeze()` method once assigned to object only allows us read operation whereas Object.seal() method once assigned to object allows us read and update operation.

Click to learn about Javascript Object.freeze() method

Object.isSealed() method

The Object.isSealed() method is used to check if the object is sealed. It returns boolean value and if object is sealed it returns true or false.

Syntax

Object.isSealed(object);

Object.seal() method using Array

Sealing Javascript Array

var names = ["Chetan", "John", "Gagan"];
Object.seal(names);
names.push("Kushal"); // Here it gives error

console.log("is array sealed : ",Object.isSealed(names));
console.log("New array data : ",names);

//Console Output
Uncaught TypeError: Cannot add property 3, object is not extensible
Check wheather the object is sealed or not using Object.isSealed() method
Check wheather the object is sealed or not using Object.isSealed() method

We cannot push new item into names array after it is sealed.It throws exception Cannot add property 3, object is not extensible.

Sealing Javascript Array Indexes

var names = ["Chetan", "John", "Gagan"];
Object.seal(names[1]); // sealed index 1 which is john
console.log("is array of index 1 sealed : ",Object.isSealed(names[1]));
names[1]=names[1].toUpperCase(); // Updating Capitalized case to Uppercase case
console.log("New array data : ",names);

//Console Output
is array of index 1 sealed :  true
New array data :  (3) ["Chetan", "JOHN", "Gagan"]
Use Object.seal() method to seal array index
Use Object.seal() method to seal array index

We can use Object.seal() method on particular index of array.

Conclusion

So its time to say bye we have come to end of our post on An Overview of Javascript Object.seal() Method for Beginners.If you like our content then please do share it and comment to clarify your doubt.

Related Posts

Summary
Review Date
Reviewed Item
An Overview of Javascript Object.seal() Method for Beginners
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development