JavaScript Basics: Object.seal and Object.isSealed

in #javascript7 years ago

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: #<Object> is not extensible
    }
    try {
        myObj.__proto__ = {
            grade: 9,
        };
        console.log(myObj.grade);
    } catch (e) {
        console.log(e); // TypeError: #<Object> 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

Sort:  

Very usefully blog

Very critical for every unknown person

so helpful post. i will try it. thanks @ghasemkiani:- for your post

superb sharing @ghasemkiani . As a #javascript learner your post always help me alot and clear my concept about different issues of JS which i faced often during #programming .

Your posts always help me alot to clear these all issues and i really wait your posts.

As this post clear the issues of javascript and well describe the javascript seal method , my question is , Is javascript freez method is same like javascript seal or both have different techniques and methods.

Thank you @ghasemkiani

Thanks for sharing this post on Java.

thanxs alot for sharing with the steemit family its really helpful!

I follow the previous post.this post is like a class basically this very helpful for me.Complete a full part, that will important for all

great post dear great writing. i will naver forget you
keep it up dear
@ghasemkiani

wow..your writing is very nice dear @ghasemkiani ,, i love your writing,,,,

I really like this post ghasemkiani! keep it up!

awesome

Coin Marketplace

STEEM 0.23
TRX 0.22
JST 0.036
BTC 98531.65
ETH 3359.79
USDT 1.00
SBD 3.16