Sub topic 12
Accessors and Private State Across Subclasses
This chapter assumes you know ES2015 class and extends, how super calls work, how the prototype chain resolves property lookups, and the basics of accessor properties and # private fields from earlier chapters. Here we look at what changes when a subclass inherits from a class that uses them.
An accessor is one property, not two
A getter and a setter with the same name form a single accessor property on the prototype. A property lookup walks the prototype chain and stops at the first object that has that key. If a subclass defines only get value(), it creates a new accessor property on Derived.prototype whose set is undefined. That property shadows the whole accessor in Base.prototype, including its setter.
'use strict';
class Base {
#value = 1;
get value() { return this.#value; }
set value(v) { this.#value = v; }
}
class Derived extends Base {
get value() { return super.value * 10; }
}
const d = new Derived();
console.log(d.value); // 10
try {
d.value = 5; // Derived.prototype.value has no setter
} catch (err) {
console.log(err instanceof TypeError); // true
}In sloppy mode the assignment fails silently, so the bug is easy to miss.
The fix: override both halves and delegate
If you override one half, write the other half too and forward it with super. super.value = v runs Base's setter with this still bound to the subclass instance.
class Base {
#value = 1;
get value() { return this.#value; }
set value(v) { this.#value = v; }
}
class Derived extends Base {
get value() { return super.value * 10; }
set value(v) { super.value = v; }
}
const d = new Derived();
d.value = 5;
console.log(d.value); // 50Private fields belong to the class that declares them
Private names are lexically scoped to their class body. A subclass instance still carries Base's #value, because Base's field initializers run on the new object when super() is called. Only code written inside Base can name that field, though. If a subclass declares a private field with the same name, it gets a separate slot.
class Base {
#secret = 'base';
revealBase() { return this.#secret; }
}
class Child extends Base {
#secret = 'child'; // a different field that happens to share the name
revealChild() { return this.#secret; }
}
const c = new Child();
console.log(c.revealBase()); // 'base'
console.log(c.revealChild()); // 'child'If Child never declared #secret, writing this.#secret inside Child would be a SyntaxError when the code is parsed. JavaScript has no protected keyword. To give subclasses controlled access, Base exposes a public (or conventionally internal) getter or method, as revealBase does here.
| Feature | Inherited by subclass instances? | Accessible by name in subclass code? |
|---|---|---|
| Public accessor | Yes, via prototype chain | Yes, through `this` or `super` |
| Base `#field` | Yes, installed by `super()` | No |
| Subclass `#field` with same name | Separate slot | Yes, only the subclass's own |
Initialization order gotcha
Subclass fields, private ones included, are installed only after super() returns. If the base constructor calls a method that the subclass overrides, and that override reads a subclass private field, the field does not exist yet.
class Base {
constructor() { this.init(); }
init() {}
}
class Child extends Base {
#ready = true;
init() { console.log(this.#ready); }
}
try {
new Child();
} catch (err) {
console.log(err instanceof TypeError); // true: #ready not yet added
}Exercise
A class Temperature has get celsius() and set celsius(v) that use a private #c. A subclass ClampedTemperature defines only set celsius(v) { super.celsius = Math.max(-273.15, v); }. What does reading instance.celsius return, and how do you fix it?
Show answer
It returns undefined. The subclass defined an accessor property with a setter and no getter, which shadows the inherited getter. Add get celsius() { return super.celsius; } to ClampedTemperature.
Exercise
Can a method defined in a subclass read a #balance field declared in its base class? What can the base class do to allow controlled access?
Show answer
No. Private names are scoped to the class body that declares them, so this.#balance in the subclass is a SyntaxError, or it refers to a separate field if the subclass declares its own #balance. The base class can expose a getter or method, such as get balance(), that reads #balance for subclasses to use.