JavaScript Basics: Object.seal and Object.isSealed

in javascript •  7 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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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

Great

Thanks for update good and informative post

I got some important things from the post, thanks for nice example.

?????
10985481_727330707411009_8738232177090416444_n.jpg

Great post as usual. Learning post

nice share .. thanks for thus us inform ton

Very important for unknown person

Programming is a hard work..It is using complex computer work slove ..Carry on dear.

This very nice post i appreciate your post thanks for sharing this technology news carry on my dear friends...

great post. It so helpful for us.
thanks for sharing

posting a very good friend

helpful post for the programmer or who wants to learn code. Thanks

very well post and awesome writing
thanks for sharing

🙏

thank you sharing for javascript programming.it is a very important part in our life.every people should be know this matter.really I have benefited to read javascript objects. its post to helped doing learning alot of thing.thank you sir for your valuable post..dear@ghasemkiani..

very important blog. Your blog is really awesome. I like it. Thanks for sharing.

thankyou for this info :)

Again good post from you, thank u

This is very educative post .Helpful for program learner .keep it up
Thanks a lot @ghasemkiani

Lots of things to learn from here. Keep it up!

I am on a trip to check on my followers, to comment, upvote and resteem post of 10 follower per day in alphabetical order. Just a means of saying thank you and showing love to the community.

videotogif_2018.01.26_01.50.05.gif

This is good. I love it, more of this.

very helpfull post...u r great sir..restem done

programming is a difficult job.keep it up. one day you will make a good programmer

nice bolg on javascript