Sub topic 15
Static Members and Inherited Constructors
This chapter assumes you know class syntax, extends, and super for instance methods, and how property lookup walks the prototype chain.
Where static members live
A static member belongs to the class itself, not to its instances. A class is a function, so a static method is a property on that function object. Instances cannot see it, because their chain runs through Animal.prototype, not through Animal.
class Animal {
constructor(name) {
this.name = name;
}
static create(name) {
return new this(name);
}
}
const cat = Animal.create('Tom');
console.log(Object.hasOwn(Animal, 'create')); // true
console.log(cat.create); // undefined
console.log(cat.constructor.create === Animal.create); // trueextends builds two chains
class Dog extends Animal sets up two prototype links. Dog.prototype links to Animal.prototype, which is how instance methods are inherited. Dog links to Animal, which is how static members are inherited.
Inside a static method, this is the constructor the method was called on. That is why new this(name) builds the right subclass:
class Animal {
constructor(name) {
this.name = name;
}
static create(name) {
return new this(name);
}
}
class Dog extends Animal {
static create(name) {
const dog = super.create(name); // calls Animal.create with this === Dog
dog.tricks = [];
return dog;
}
}
const rex = Dog.create('Rex');
console.log(Object.getPrototypeOf(Dog) === Animal); // true
console.log(rex instanceof Dog); // true
console.log(rex.tricks); // []In a static method, super.create looks up create on Animal, the prototype of Dog, and keeps this bound to Dog.
Common error: static state and shadowing
Static fields follow the same shadowing rule as instance properties. Reading a field finds it through the chain, but writing to it creates an own property on whichever constructor this refers to.
class Base {
static count = 0;
static increment() {
this.count++;
return this.count;
}
}
class Child extends Base {}
console.log(Child.increment()); // 1
console.log(Base.count); // 0
console.log(Object.hasOwn(Child, 'count')); // truethis.count++ read the inherited 0 from Base, then wrote 1 as a new own property on Child. If you want a single counter shared by the whole hierarchy, name the class explicitly with Base.count++. If you want one counter per class, the code above already does that, but every subclass starts from whatever Base.count held when the subclass first wrote.
Constructor functions do not inherit statics automatically
In the pre-class pattern, Dog.prototype = Object.create(Animal.prototype) links only the instance chain. To inherit statics as well, you also have to link the constructors with Object.setPrototypeOf(Dog, Animal). extends does this for you.
| Link | Set by `extends` | Pre-class equivalent |
|---|---|---|
| `Dog.prototype` → `Animal.prototype` | Yes | `Object.create(Animal.prototype)` |
| `Dog` → `Animal` | Yes | `Object.setPrototypeOf(Dog, Animal)` |
Exercise
Given class A { static make() { return new this(); } } and class B extends A {}, what does B.make() instanceof B return, and why?
Show answer
It returns true. B.make is found on A through the link Object.getPrototypeOf(B) === A, and inside the method this is B, so new this() constructs a B.
Exercise
A class has static registry = [] and a static method that runs this.registry.push(item). Called on a subclass, does it create a separate array for the subclass?
Show answer
No. this.registry reads the inherited array from the parent and push mutates that array, so the whole hierarchy shares one array. Only an assignment such as this.registry = [...] would create an own property on the subclass.