Object.seal( ) In JavaScript

09 Nov, 2021
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 that 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: 
Javascript
function Automobile(color) {  this.color=color;} var vehicle1 = new Automobile ("red");
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: 
It creates a fresh new object(instance) Automobile() and assigns it to a variable.It sets the constructor property i.e “color” of the object to Automobile.
It creates a fresh new object(instance) Automobile() and assigns it to a variable.
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: 
obj : It is the object which has to be sealed.
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 existent 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: 
Javascript
<script>// creating an object constructor and assigning values to itconst obj1 = { property1: 'initial_data'}; // creating a second object which will seal the properties of the first objectconst obj2 = Object.seal(obj1); // Updating the properties of the frozen objectobj2.property1 = 'new_data'; // Displaying the properties of the  frozen objectconsole.log(obj2.property1);</script>
OUTPUT: 
"new_data"
Code 2: 
Javascript
<script>// creating an object constructor and assigning values to itvar obj = { prop: function() {}, name: 'adam' }; // Displaying the properties of the object createdconsole.log(obj); // Updating the properties of the objectobj.name = 'billy';delete obj.prop; // Displaying the updated properties of the objectconsole.log(obj); // Sealing the object using object.seal() methodvar o = Object.seal(obj); // Updating the properties of the objectdelete obj.prop; // Displaying the updated properties of the objectconsole.log(obj); // Updating the properties of the sealed objectobj.name = 'chris'; // Displaying the properties of the  frozen objectconsole.log(obj); </script>
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.
Supported Browsers:
Google Chrome 6.0 and above
Internet Explorer 9.0 and above
Mozilla 4.0 and above
Opera 12 and above
Safari 5.0 and above
Edge 12 and above
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal 
ysachin2314
clintra
javascript-functions
javascript-object
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
How to get character array from string in JavaScript?
JavaScript | console.log() with Examples
JavaScript | Promises