Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Freezing JavaScript Objects With Object.freeze()

DZone's Guide to

Freezing JavaScript Objects With Object.freeze()

In this post, we look at how to 'freeze' objects using JavaScript, so you can prevent the modification of existing attributes and values, or the addition of properties.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

In JavaScript, objects are used to store keyed collections of various data and more complex entities. Objects penetrate almost every aspect of the JavaScript language.

The object might be accessed as global or passed as an argument. Functions that have access to the object can modify the object, whether intentionally or accidentally. To prevent modification of our objects, one of the techniques is to use Object.freeze(). This method prevents the modification of existing property attributes and values and prevents the addition of new properties.

Ok, why would I need to freeze an object?

Freezing an object can be useful for representing a logically immutable data structure, especially if changing the properties of the object could lead to bad behavior elsewhere in your application.

Let’s look at an example:

var employee = {
    name: "John Doe",
    role: "Developer"
};

Object.freeze(employee);

(function() {
    "use strict";

    // TypeError: Can't modify property role
    employee.role = "Tester";
})();

Since we have added strict mode, we will see the TypeError as shown below.

Image title

Let’s see another example:

var employee = {
    name: "John Doe",
    role: "Developer"
};

Object.freeze(employee);

(function() {
    "use strict";

    // TypeError: Can't add property department, object is not extensible
    employee.department = "IT";
})();

And the output would be as follows:

Image title

The Object.freeze method takes an object and renders it immutable. In the example above, the employee object remains safe from modification and available for later use.

We don’t want to throw errors all over the place, so JavaScript provides another method, Object.isFrozen(), to detect whether the object is frozen.

var employee = {
    name: "John Doe",
    role: "Developer"
};

Object.freeze(employee);

if (Object.isFrozen(employee)) {
    alert("employee is frozen!");
}

And you will see the result as follows:

Image title


Object.freeze is shallow, so you'd need to recursively apply it on nested objects to protect them. It is part of the ECMAScript 5 specification, which means it isn't available in older browsers like IE8 and below.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
javascript ,es5 ,es6 ,web dev

Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}