DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Freezing JavaScript Objects With Object.freeze()

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.

Swathi Prasad user avatar by
Swathi Prasad
·
Apr. 18, 17 · Web Dev Zone · Tutorial
Like (7)
Save
Tweet
7.39K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Object (computer science) JavaScript

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Translate Value to Executives Using an Outcome-Driven Mindset
  • Flask vs. Django: Which Python Framework to Choose?
  • Top ALM Tools and Solutions Providers
  • Change Data Capture to Accelerate Real-Time Analytics

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo