Sub topic 8
Class Instances, Getters and Other Things That Do Not Survive
This chapter assumes you know JavaScript classes, property descriptors (Object.defineProperty), symbols, and the basic structuredClone call covered earlier. The focus here is what a successful clone quietly leaves behind. Unsupported values make structuredClone throw an error. The losses in this chapter happen silently.
The rule behind the losses
The structured clone algorithm treats an ordinary object as a bag of data. For each object it visits, it reads the own, enumerable, string-keyed properties. It reads each value, getters included, clones that value, and writes it onto a brand-new plain object. Anything outside that rule is not copied.
Prototypes and class identity
The copy's prototype is always Object.prototype. Methods live on the class prototype, so they are gone, and so are instanceof checks.
class Point {
constructor(x, y) { this.x = x; this.y = y; }
get length() { return Math.hypot(this.x, this.y); }
}
const copy = structuredClone(new Point(3, 4));
console.log(copy instanceof Point); // false
console.log(copy); // { x: 3, y: 4 }
console.log(copy.length); // undefined (the getter lived on Point.prototype)The length getter is defined on Point.prototype, not on the instance, so it is never read. Only x and y, which are own properties, survive.
Private fields (#field) are not properties at all, so they disappear too:
class Account {
#balance = 100;
constructor(owner) { this.owner = owner; }
}
console.log(structuredClone(new Account('Ada'))); // { owner: 'Ada' }Getters become frozen values
An accessor defined directly on the object is invoked once during cloning. The copy stores whatever the getter returned at that moment, as a plain data property.
let counter = 0;
const source = { get next() { return ++counter; } };
const copy = structuredClone(source);
console.log(Object.getOwnPropertyDescriptor(copy, 'next'));
// { value: 1, writable: true, enumerable: true, configurable: true }
console.log(copy.next, copy.next); // 1 1 (no longer computed)Setters are lost completely. If a getter throws, the whole structuredClone call throws that error.
Descriptors, non-enumerable properties and symbols
Every copied property becomes writable, enumerable and configurable. Non-enumerable properties and symbol-keyed properties are skipped. Object-level states such as frozen, sealed or non-extensible are not carried over either.
const sym = Symbol('id');
const source = { visible: 1, [sym]: 2 };
Object.defineProperty(source, 'hidden', { value: 3, enumerable: false });
Object.defineProperty(source, 'locked', { value: 4, enumerable: true, writable: false });
Object.freeze(source);
const copy = structuredClone(source);
console.log(copy); // { visible: 1, locked: 4 }
console.log(Object.getOwnPropertySymbols(copy)); // []
console.log(Object.isFrozen(copy)); // false
console.log(Object.getOwnPropertyDescriptor(copy, 'locked').writable); // true| Feature on the source | Result in the copy |
|---|---|
| Class prototype and methods | Replaced by `Object.prototype` |
| Private `#fields` | Dropped |
| Own getter | Called once, stored as a plain value |
| Setter | Dropped |
| Non-enumerable property | Dropped |
| Symbol-keyed property | Dropped |
| `writable: false`, `configurable: false` | Reset to `true` |
| Frozen or sealed object | Copy is mutable |
Restoring class instances
If you need real instances back, rebuild them yourself after cloning. A small static factory keeps this explicit:
class Point {
constructor(x, y) { this.x = x; this.y = y; }
get length() { return Math.hypot(this.x, this.y); }
static from(data) { return new Point(data.x, data.y); }
}
const original = new Point(3, 4);
const restored = Point.from(structuredClone(original));
console.log(restored instanceof Point); // true
console.log(restored.length); // 5A shortcut is Object.assign(Object.create(Point.prototype), plainCopy). It restores the methods, but it skips the constructor, so it cannot rebuild private fields or run any validation. For classes with private state, use a factory or a clone() method instead.
Exercise
Given const o = { a: 1, get b() { return this.a * 2; } }; Object.defineProperty(o, 'c', { value: 3 }); o[Symbol('d')] = 4;, what does structuredClone(o) return, and what does the copy's b evaluate to after you set copy.a = 10?
Show answer
It returns { a: 1, b: 2 }. The property c is non-enumerable (defineProperty defaults to false) and the symbol key is skipped. The getter ran once during cloning, so b is a plain data property holding 2 and stays 2 after copy.a = 10.
Exercise
A Temperature class stores its reading in a private field #celsius and exposes it through a prototype getter celsius. Why does structuredClone(new Temperature(21)) return {}, and how would you produce a working copy?
Show answer
The private field is not a property, and the getter lives on the prototype rather than the instance, so the instance has no own enumerable string-keyed properties to copy. Add a method such as clone() { return new Temperature(this.#celsius); }, or serialize the reading explicitly and rebuild with the constructor.