Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Why Object.freeze() Doesn’t Stop Nested Properties From Being Changed

- 26.09.26 - ErcanOPAK

⚡ Frozen on the Outside, Fully Mutable on the Inside

`Object.freeze()` looks like it locks an object down completely, and attempts to reassign its top-level properties do get silently ignored (or throw, in strict mode) – but freeze is shallow: any object or array nested inside the frozen object is a completely separate object reference, and freezing the parent does absolutely nothing to it.

🔎 The Problem

const config = Object.freeze({
  name: "checkout-service",
  limits: { maxRetries: 3, timeoutMs: 5000 },
});

config.name = "something-else";        // silently ignored - top-level, frozen
console.log(config.name);              // "checkout-service" - unchanged, as expected

config.limits.maxRetries = 99;         // NOT ignored - `limits` was never frozen
console.log(config.limits.maxRetries); // 99 - the "frozen" config just changed

✅ Fix: Freeze Every Level That Needs to Be Immutable

  • Write a small recursive `deepFreeze()` helper that calls `Object.freeze()` on the object itself and then recurses into every property that’s an object or array – this is the only way `Object.freeze()` actually reaches nested structures, since it was never designed to do so on its own.
  • For anything beyond a small config object, a library built for this (Immer, or the built-in `structuredClone()` combined with treating the clone as read-only by convention) scales much better than a hand-rolled deep freeze, especially for deeply nested or frequently-updated state.
  • In TypeScript, `Readonly<T>` (or a recursive `DeepReadonly<T>` utility type) catches attempted nested mutations at compile time – which is a stronger guarantee than `Object.freeze()` alone provides, since freeze is a runtime-only, shallow protection.

⚠️ Why the Bug Hides Until State Actually Depends on It

  • A `console.log` of the frozen object right after creation shows exactly what you expect, because nothing has mutated yet – the bug only becomes visible once some later code path reaches in and changes a nested value, often far away from where the object was originally frozen.
  • This is a common source of confusing behavior in Redux-style state management, where reducers are expected to treat state as immutable – a shallow freeze gives a false sense of safety while a nested object is still silently mutated in place.

Object.freeze() protects exactly one layer – everything nested one level deeper is exactly as mutable as it would’ve been without freeze at all.

— JavaScript Tip

Related posts:

JavaScript: Master ES6 Classes for OOP

JavaScript: Master ES6 Classes for OOP

JavaScript Objects Grow Without You Noticing

JavaScript Objects Grow Without You Noticing

JavaScript Async/Await: Complete Guide to Clean Promise Chains

JavaScript Async/Await: Complete Guide to Clean Promise Chains

Views: 7

Post navigation

WordPress: Fix a Mixed-Content Warning That Appears Right After Moving to HTTPS
Windows 11: Fix a Wi-Fi Network That Shows Connected but Has No Actual Internet Access

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com