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: Fix structuredClone Dropping a Class’s Methods

- 19.11.26 - ErcanOPAK

⚡ Fix structuredClone Dropping a Class’s Methods

`structuredClone()` implements the HTML structured clone algorithm, which deep-copies DATA — plain objects, arrays, Maps, Sets, Dates — but does not preserve a custom class’s prototype chain. Cloning an instance of a class with methods produces a plain object with the same own properties but a prototype reset to `Object.prototype`, so every method defined on the class is simply gone from the clone — invisible until code calls `clone.someMethod()` and gets a `TypeError`, often far from wherever the clone itself was made.

🔎 The Problem

class Order {
  constructor(id, total) { this.id = id; this.total = total; }
  formattedTotal() { return `$${this.total.toFixed(2)}`; }
}

const order = new Order(42, 19.5);
const bad = structuredClone(order);
// bad.formattedTotal is undefined - the prototype was not preserved

// Fix: clone the plain data, then re-attach the class
const fixed = Object.assign(
  new Order(0, 0),
  structuredClone({ id: order.id, total: order.total })
);
fixed.formattedTotal(); // "$19.50"

✅ Fix: Clone the Data, Rebuild the Instance

  • Treat `structuredClone()` as a data cloner, not an object cloner — call it only on the plain-data shape of a class instance, then reconstruct a real instance from that data rather than passing the instance itself and hoping its methods survive.
  • If a class genuinely needs to be clone-aware, expose a plain `toData()` method and a matching static `fromData()` constructor, and always route cloning through that pair instead of calling `structuredClone` directly on instances.
  • Add a runtime guard right after cloning a complex object — a quick check that a known method is still a function — so a lost prototype surfaces immediately as a clear error instead of a silent `undefined` that only breaks later.

🚨 Why This Is Dangerous

  • Because the clone still looks correct in a `console.log()` — all the data is there, it just prints as a plain object — this failure mode is easy to miss in a quick manual check and tends to surface for the first time in production.

structuredClone promises to copy your data faithfully – it never promised to copy your class along with it.

— JavaScript Tip

Related posts:

JavaScript: Use Web Workers for Multi-threading

JavaScript: Use Web Workers for Multi-threading

JavaScript: Fix a WeakRef Target That Gets Collected Earlier Than Expected Because Nothing Else Hold...

JavaScript: Fix a WeakRef Target That Gets Collected Earlier Than Expected Because Nothing Else Hold...

JS Memory Leaks — Forgotten Event Listeners

JS Memory Leaks — Forgotten Event Listeners

Views: 4

Post navigation

Windows 11: Fix a USB-C Dock Dropping Displays After Wake
CSS: Fix a Sticky Header Jumping When a Scrollbar Appears

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,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

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

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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