Skip to content

Object.seal()

Why Object.seal ?

Object.seal () is a method used to seal the object. Sealing an object: does not allow the addition of new features and preserves all existing features. Does not allow configuration. The object to be sealed is passed as an argument and the method returns the sealed object.

Syntax:

Object.seal( obj )  

  • obj: It is the object which should be sealed.

Return value:

  • Object.sealed() returns the object that was passed to the function.

Object.seal() VS Object.freeze()

Object.freeze () and Object.seal () are related to the invariance of the object. The main difference between the two is that in Object.seal () we can change the values of the defined properties. The methods of checking whether an object is frozen or sealed are intuitive and give a boolean value: Object.isFrozen () and Object.isSealed ().

If an object is frozen using the Object.freeze () method, its properties become unchanged and no changes can be made to them. But if the Object.seal () method is used, the changes are present.


JavaScript Object.seal Examples

Example 1

In the following example, we create an object named yourName. We give it a name named “name”. Then, with Object.seal (), we assign yourName to a variable named arr. Then we return the arr variable.

        let yourName = {
            name: "alex"
        };

        let arr = Object.seal(yourName);
        delete arr.name;
        console.log(arr)

output:

{name: “alex”}

As you can see, the name “name” has not been deleted.


Example 2

        const ages = {
            age1: 32
        };
        Object.seal(ages);

        ages.age1 = 45;
        console.log(ages.age1);
        delete ages.age1;
        console.log(ages.age1);

output:

45
45


Example 3

        var obj = { prop: function () { }, name: 'alex' };

        console.log(obj);

        obj.name = 'barry';
        delete obj.prop;
        console.log(obj);

        var o = Object.seal(obj);

        delete obj.prop;
        console.log(obj);

        obj.name = 'rick';
        console.log(obj);

output:

{prop: ƒ, name: “alex”}
{name: “barry”}
{name: “barry”}
{name: “rick”}


Example 4

        let poeple = {
            person1: "alex"
        }

        poeple.person2 = "rick";
        console.log(poeple);

        Object.seal(poeple);
        poeple.person3 = "bob";
        console.log(poeple);

output:

{person1: “alex”, person2: “rick”}
{person1: “alex”, person2: “rick”}


Sources:


Browser Support

Chrome6
Edgeyes
Firefox4
Internet Explorer9
Opera12
Safari5.1
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: