JavaScript - isSealed() method

revision:


Category : object

The Object.isSealed() static method determines if an object is sealed.

Syntax :

        Object.isSealed(obj)
    

Parameters:

obj : required. The object which should be checked.

: optional. .

Examples:

        const object1 = {
            property1: 42
        };
        console.log(Object.isSealed(object1));
          // Expected output: false
        Object.seal(object1);
        console.log(Object.isSealed(object1));
          // Expected output: true
          
    

Practical examples

example: using Object.isSealed() method

code:
                    <div>
                        <p id="seal-1"></p>
                        <p id="seal-2"></p>
                        <p id="seal-3"></p>
                        <p id="seal-4"></p>
                        <p id="seal-5"></p>
                        <p id="seal-6"></p>
                        <p id="seal-7"></p>
                        <p id="seal-8"></p>
                        <p id="seal-9"></p>
                    </div>
                    <script>
                        // Objects aren't sealed by default.
                        const empty = {};
                        Object.isSealed(empty); // false
                        document.getElementById("seal-1").innerHTML = "is sealed ? : " + Object.isSealed(empty);
                        // If you make an empty object non-extensible, it is vacuously sealed.
                        Object.preventExtensions(empty);
                        Object.isSealed(empty); // true
                        document.getElementById("seal-2").innerHTML = "is sealed ? : " + Object.isSealed(empty);
                        // The same is not true of a non-empty object, unless its properties are all non-configurable.
                        const hasProp = { fee: "fie foe fum" };
                        Object.preventExtensions(hasProp);
                        Object.isSealed(hasProp); // false
                        document.getElementById("seal-3").innerHTML = "is sealed ? : " + Object.isSealed(hasProp);
                        // But make them all non-configurable and the object becomes sealed.
                        Object.defineProperty(hasProp, "fee", {
                            configurable: false,
                        });
                        Object.isSealed(hasProp); // true
                        document.getElementById("seal-4").innerHTML = "is sealed ? : " + Object.isSealed(hasProp);
                        // The easiest way to seal an object, of course, is Object.seal.
                        const sealed = {};
                        Object.seal(sealed);
                        Object.isSealed(sealed); // true
                        document.getElementById("seal-5").innerHTML = "is sealed ? : " + Object.isSealed(sealed);
                        // A sealed object is, by definition, non-extensible.
                        Object.isExtensible(sealed); // false
                        document.getElementById("seal-6").innerHTML = "is sealed object extensible? : " + Object.isExtensible(sealed);
                        // A sealed object might be frozen, but it doesn't have to be.
                        Object.isFrozen(sealed); // true
                        // (all properties also non-writable)
                        document.getElementById("seal-7").innerHTML = "is sealed object frozen? : " + Object.isFrozen(sealed);
                        const s2 = Object.seal({ p: 3 });
                        Object.isFrozen(s2); // false
                        // ('p' is still writable)
                        document.getElementById("seal-8").innerHTML = "is sealed object frozen? : " + Object.isFrozen(s2);
                        const s3 = Object.seal({
                        get p() {
                            return 0;
                        },
                        });
                        Object.isFrozen(s3); // true
                        // (only configurability matters for accessor properties)
                        document.getElementById("seal-9").innerHTML = "is sealed object frozen? : " + Object.isFrozen(s3);
                    </script>
                

example: object has not been sealed using the Object.seal() method.

code:
                    <div>
                        <p id="seal-10"></p>
                        <p id="seal-11"></p>
                        <p id="seal-12"></p>
                    </div>
                    <script>
                        // creating an object constructor and assigning values to it
                        const object = {
                            property: 'hi ninjasforninjas'
                        };
                        // checking whether the object is sealed or not
                        console.log(Object.isSealed(object));
                        document.getElementById("seal-10").innerHTML = "is object sealed? : " + Object.isSealed(object);
                        // Using seal() method to seal the object
                        Object.seal(object);
                        // checking whether the object is frozen or not
                        console.log(Object.isSealed(object));
                        document.getElementById("seal-11").innerHTML = "is object sealed? : " + Object.isSealed(object);
                    </script>