Sub topic 16
Static Members and Inherited Constructors
This chapter assumes you already know how extends links Child.prototype to Parent.prototype, how super calls parent methods, and how shadowing works when you assign to an inherited property. Here the focus moves from instances to the constructors themselves. When you write class Dog extends Animal, JavaScript builds a second prototype chain that links the classes directly, and static members are looked up along that chain.
Where Static Members Live
A static member is a property of the constructor function, not of its prototype object. Instances never see it, because the instance chain goes through Animal.prototype, not through Animal.
class Animal {
static kingdom = 'Animalia';
static create(name) {
return new this(name);
}
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {}
const rex = Dog.create('Rex');
console.log(Dog.kingdom); // 'Animalia'
console.log(Object.hasOwn(Dog, 'kingdom')); // false
console.log(Object.hasOwn(Animal, 'kingdom')); // true
console.log(rex.kingdom); // undefined
console.log(Object.getPrototypeOf(Dog) === Animal); // true
console.log(Object.getPrototypeOf(Animal) === Function.prototype); // true
console.log(rex instanceof Dog); // trueDog has no kingdom or create of its own. Looking up Dog.kingdom fails on Dog, follows Dog's [[Prototype]] to Animal, and finds it there. A base class with no extends has Function.prototype as its prototype, which is why every class can use call, apply, and bind.
Two Parallel Chains
Every extends sets two links: one between the prototype objects (used by instances) and one between the constructors (used by static lookup).
| Aspect | Instance members | Static members |
|---|---|---|
| Stored on | `Class.prototype` (methods) or the instance (fields) | The class function itself |
| Inherited via | `Child.prototype → Parent.prototype` | `Child → Parent` |
| `this` inside a method | The instance | The class the method was called on |
| `super.x` refers to | `Parent.prototype.x` | `Parent.x` |
| Typical use | Per-object behavior | Factories, registries, constants, utilities |
`this` in Static Methods Is the Calling Class
Static methods follow the usual rule for this: it is whatever sits before the dot. Dog.create('Rex') runs Animal's create with this === Dog, so new this(name) builds a Dog. If you hard-code new Animal(name), subclasses lose that behavior.
Built-ins work the same way. Array.from and Array.of use this as the constructor:
class Stack extends Array {
peek() {
return this[this.length - 1];
}
}
const s = Stack.from([1, 2, 3]);
console.log(s instanceof Stack); // true
console.log(s.peek()); // 3`super` in Static Methods
Inside a static method, super refers to the parent constructor, and this stays bound to the class you called.
class Shape {
static describe() {
return `Shape(${this.name})`;
}
}
class Circle extends Shape {
static describe() {
return `${super.describe()} -> round`;
}
}
console.log(Circle.describe()); // "Shape(Circle) -> round"this.name is 'Circle' because every function's name property holds its class name, and this is still Circle when super.describe() runs.
Gotcha: Static Fields Get Shadowed by Subclasses
Static data fields are ordinary properties on the parent class, so the shadowing rules apply. Reading through this finds the parent's value, but writing through this creates an own property on the subclass.
class Model {
static count = 0;
static track() {
this.count++;
return this.count;
}
}
class User extends Model {}
User.track();
User.track();
Model.track();
console.log(User.count); // 2
console.log(Model.count); // 1
console.log(Object.hasOwn(User, 'count')); // trueThe first User.track() reads Model.count (0) through the chain, then assigns User.count = 1, which creates a new own property. Each class ends up with its own counter. Whether that is a bug depends on what you meant:
- For one shared counter, write
Model.count++explicitly. - For a counter per class, the code above works. Say so in a comment, because readers will expect a shared counter.
Gotcha: Private Static Members Are Not Inherited
Private names (#x) are not looked up through the prototype chain. A static private field exists only on the class that declares it, so accessing it through this from a subclass throws.
class Base {
static #registry = [];
static register(item) {
this.#registry.push(item);
return this.#registry.length;
}
static safeRegister(item) {
Base.#registry.push(item);
return Base.#registry.length;
}
}
class Sub extends Base {}
console.log(Base.register('a')); // 1
try {
Sub.register('b');
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(Sub.safeRegister('c')); // 2To be safe from subclass calls, refer to static private members through the class name, not through this.
The Pre-Class Equivalent
Before classes, static members were plain properties assigned to the constructor function. Linking the prototypes with Object.create gave instances inheritance, but statics were not inherited unless you also linked the constructors. Much older code skipped that step.
function Animal(name) {
this.name = name;
}
Animal.create = function (name) {
return new this(name);
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Object.defineProperty(Dog.prototype, 'constructor', {
value: Dog, writable: true, configurable: true, enumerable: false
});
Object.setPrototypeOf(Dog, Animal); // the step classes do for you
const rex = Dog.create('Rex');
console.log(rex instanceof Dog); // true
console.log(rex.name); // 'Rex'If you remove the Object.setPrototypeOf line, Dog.create is undefined. Calling setPrototypeOf once at definition time, before any instances exist, is fine. The performance concerns in a later chapter apply to changing prototypes on objects that are already in use.
Static Initialization Blocks
When a static field needs setup logic, use a static { } block. It runs once, when the class is defined, with this bound to that class. A parent's block does not run again for subclasses.
class Config {
static defaults;
static {
this.defaults = Object.freeze({ retries: 3, timeout: 1000 });
}
}
class StrictConfig extends Config {}
console.log(StrictConfig.defaults.retries); // 3
console.log(Object.hasOwn(StrictConfig, 'defaults')); // falseExercise
Given class A { static greet() { return 'A:' + this.name; } } and class B extends A {}, what does B.greet() return, and what is Object.getPrototypeOf(B)?
Show answer
It returns "A:B". greet is found on A through B's prototype chain, but this is B, so this.name is "B". Object.getPrototypeOf(B) is A itself, not A.prototype.
Exercise
A base class declares static instances = 0 and its constructor runs this.constructor.instances++. After creating two Child objects and one Parent object, what are Parent.instances and Child.instances, and why?
Show answer
Parent.instances is 1 and Child.instances is 2. The first Child construction reads the inherited 0 and assigns Child.instances = 1, which creates an own property that shadows Parent's. Later Child constructions update Child's own property. The Parent construction updates only Parent's.
Exercise
Why does calling a subclass's inherited static method throw a TypeError when the method reads this.#cache, and how do you fix it?
Show answer
Private names are not resolved through the prototype chain. The subclass constructor does not have the #cache slot, so the access throws. Fix it by reading ParentClass.#cache explicitly, or by declaring a separate private static field in each subclass that needs one.