JavaScript - seal() method

revision:


Category : object

The Object.seal() static method seals an object. Sealing an object prevents extensions and makes existing properties non-configurable. A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned. Values of existing properties can still be changed as long as they are writable. seal() returns the same object that was passed in.

Syntax :

        Object.seal(obj)
    

Parameters:

obj : required. The object which should be sealed.

Examples:

        const object1 = {
            property1: 42
        };
        Object.seal(object1);
        object1.property1 = 33;
        console.log(object1.property1);
        // Expected output: 33
        delete object1.property1; // Cannot delete when sealed
        console.log(object1.property1);
        // Expected output: 33
    

Practical examples

example: properties on a sealde object.

code:
                    <div>
                        <p id="seal-1"></p>
                        <p id="seal-2"></p>
                        <p id="seal-3"></p>
                        
                    </div>
                    <script>
                        const obj = {
                            prop() {},
                            foo: "bar",
                        };
                        // New properties may be added, existing properties may be changed or removed.
                        obj.foo = "baz";
                        obj.lumpy = "woof";
                        delete obj.prop;
                        const o = Object.seal(obj);
                        o === obj; // true
                        document.getElementById("seal-1").innerHTML = "o === obj : " + (o === obj);
                        Object.isSealed(obj); // true
                        document.getElementById("seal-2").innerHTML = "obj is sealed ?: " + Object.isSealed(obj);
                        // Changing property values on a sealed object still works.
                        obj.foo = "quux";
                        document.getElementById("seal-3").innerHTML = "changing values of obj ?: " + obj.foo;
                        // But you can't convert data properties to accessors, or vice versa.
                        Object.defineProperty(obj, "foo", {
                            get() {
                                return "g";
                            },
                        }); // throws a TypeError
                        // Now any changes, other than to property values will fail.
                        obj.quaxxor = "the friendly duck";
                        // silently doesn't add the property
                        delete obj.foo;
                        // silently doesn't delete the property
                        // ...and in strict mode such attempts will throw TypeErrors.
                        function fail() {
                            "use strict";
                            delete obj.foo; // throws a TypeError
                            obj.sparky = "arf"; // throws a TypeError
                            }
                        fail();
                                // Attempted additions through
                        // Object.defineProperty will also throw.
                        Object.defineProperty(obj, "ohai", {
                        value: 17,
                        }); // throws a TypeError
                        Object.defineProperty(obj, "foo", {
                        value: "eit",
                        }); // changes existing property value
                    </script>
                

example: the Object.seal() method returns the object which has been sealed.

code:
                    <div>
                        <p id="seal-4"></p>
                        <p id="seal-5"></p>
                        <p id="seal-6"></p>
                    </div>
                    <script>
                        const obj1 = { property1: 'Marry'};  
                        const obj2 = Object.seal(obj1);  
                        // prevents other code from deleting properties of an object.  
                        obj2.property1 = 'carry';  
                        console.log(obj2.property1);  
                        document.getElementById("seal-4").innerHTML = "property after sealing : " + obj2.property1;
            
                        const object1 = { property1: 29 };  
                        Object.seal(object1);  
                        // Prevents other code from deleting properties of an object.  
                        object1.property1 =45;  
                        console.log(object1.property1);  
                        document.getElementById("seal-5").innerHTML = "property after sealing : " + object1.property1;
                        delete object1.property1;  
                        // cannot delete when sealed  
                        document.getElementById("seal-6").innerHTML = "property after sealing  and deleting: " + object1.property1;
                    </script>
                

example: after sealing, new values cannot be added.

code:
                    <div>
                        <p id="seal-7"></p>
                        <p id="seal-8"></p>
                    </div>
                    <script>
                        // creating an object constructor and assigning values to it 
                        const obj_1 = { property1: 'initial_data' };
                        document.getElementById("seal-7").innerHTML = "object property : " + JSON.stringify(obj_1);
                        // creating a second object which will seal the properties of the first object
                        const obj_2 = Object.seal(obj_1);
                        // Updating the properties of the frozen object
                        obj_2.property1 = 'new_data';
                        // Displaying the properties of the  frozen object 
                        console.log(obj_2.property1);
                        document.getElementById("seal-8").innerHTML = "object property : " + JSON.stringify(obj_2.property1);
                    </script>