Search Here

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

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 be updated.

Syntax

Object.seal(object);

The Object.seal() Method takes an object as a parameter and returns a 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 the above example, we have a fruit object which has a name and color property. After sealing that object we added a new property weight and new method get_name() to the fruit object. But throws an 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 to read operation whereas Object.seal() method once assigned to object allows us to 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 a boolean value and if the 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 whether the object is sealed or not using the Object.isSealed() method

We cannot push a new item into names array after it is sealed. It throws an 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 the particular index of the array.

Conclusion

So it’s time to say bye we have come to the 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