JavaScript Basics: Object.seal and Object.isSealed

In the previous post, I wrote about Object.preventExtensions. That functions prevents the addition of new properties to an object. However, it does not prevent you from configuring, mutating, or even deleting the existing properties.

The following code snippet demonstrates the workings of Object.preventExtensions:

    let myObj = {
        name: "Ali",
    };
    Object.preventExtensions(myObj);
    // You can still configure the existing properties
    Object.defineProperty(myObj, "name", {
        get() {
            return "Susan";
        },
    });
    console.log(myObj.name); // Susan
    // You can even delete the existing properties
    delete myObj.name;
    console.log(myObj.name); // undefined
    
    // You just can't add new properties.
    // The following code throws an exception:
    // TypeError: Cannot define property age, object is not extensible
    Object.defineProperty(myObj, "age", {
        value: 19,
    });

What if we want to prevent the existing properties from being configured again?

For this purpose, you can use Object.seal. This function prevents adding new properties and prevents the existing properties from being reconfigured or deleted. After calling this function, the only thing you can do is to change the values of the existing properties. You cannot change their definition. For example, you cannot convert a data property into an accessor property or vice versa.

Here is an example:

    let myObj = {
        name: "Ali",
    };
    Object.seal(myObj);
    
    console.log(Object.isSealed(myObj)); // true

    // You cannot add new properties. This fails silently.
    myObj.age = 19;
    console.log(myObj.age); // undefined

    // You cannot reconfigure existing properties.
    try {
        Object.defineProperty(myObj, "name", {
            get() {
                return "Susan";
            },
        });
        console.log(myObj.name);
    } catch (e) {
        console.log(e); // TypeError: Cannot redefine property: name
    }

    // You can still change property values.
    myObj.name = "Susan";
    console.log(myObj.name); // Susan
    // This is also okay (since this only changes the value of the property
    // and does not change its configuration):
    Object.defineProperty(myObj, "name", {
        value: "Aria",
    });
    console.log(myObj.name); // Aria

    // You can add new properties to the prototype.
    myObj.__proto__.age = 14;
    console.log(myObj.age); // 14

    // You cannot change the prototype.
    try {
        Object.setPrototypeOf(myObj, {
            grade: 9,
        });
        console.log(myObj.grade);
    } catch (e) {
        console.log(e); // TypeError: # is not extensible
    }
    try {
        myObj.__proto__ = {
            grade: 9,
        };
        console.log(myObj.grade);
    } catch (e) {
        console.log(e); // TypeError: # is not extensible
    }

In short, Object.seal prevents extensions to the object and makes all the existing properties non-configurable, but does not prevent the values of the existing properties from being changed.


Related Posts

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center