DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. 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 · Tutorial
Like (7)
Save
Tweet
Share
7.62K 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

  • Spring Boot Docker Best Practices
  • Project Hygiene
  • Beginners’ Guide to Run a Linux Server Securely
  • A Beginner's Guide to Back-End Development

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: