CKTIS / JavaScript
Class constructor method (`constructor`)
Use `constructor` to set up an object created with `new`. In a derived class, `super()` makes `this` available.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
class Base {
constructor(value) {
this.value = value;
}
}
class Derived extends Base {
constructor(value, extra = 0) {
super(value);
this.extra = extra;
}
}
new Base(1);
new Derived(1, 2);The Class constructor method is the special constructor definition in a class body. In new ClassName(...), it receives the supplied arguments and can set the object's initial state, for example with this.value = value. Calling ClassName(...) as an ordinary function without new throws a TypeError.
A class can declare at most one constructor. A second declaration is a SyntaxError, even with a different parameter list. Use default or rest parameters (...args) instead of overloading. The constructor cannot be async, a generator, a getter, or a setter. Neither static constructor() nor ["constructor"]() defines the class constructor. The property ClassName.prototype.constructor normally points to the class itself; it is not a separate initialization method.
If you omit the constructor, JavaScript supplies one. In a base class (no extends), the default does no extra initialization beyond the declared instance fields. In a derived class with a superclass constructor, the default forwards all arguments to that constructor without using an array iterator, unlike an explicit super(...args). A class declared with extends null is still derived, but there is no superclass constructor to call, so instantiating it with the default constructor throws a TypeError.
In an explicit derived constructor, super(...) invokes the superclass constructor and binds this to the object it returns. Accessing this beforehand throws a ReferenceError. Base instance fields initialize before the base constructor body runs. Derived instance fields initialize after the superclass constructor returns and before execution continues past super(...).
An explicitly returned object becomes the result of new. A base constructor ignores primitive return values; a derived constructor throws a TypeError for any primitive return value other than undefined. A derived constructor can skip super(...) and succeed only by explicitly returning an object: falling through or returning undefined without an initialized this throws a ReferenceError.
Examples
{
class Ticket {
constructor(id) {
this.id = id;
}
}
class PriorityTicket extends Ticket {
label = `#${this.id}`;
constructor(id, priority = "normal") {
super(id);
this.priority = priority;
}
}
const first = new PriorityTicket(7, "high");
const second = new PriorityTicket(8);
console.log(`${first.label}: ${first.priority}`);
console.log(`${second.label}: ${second.priority}`);
}Result
#7: high #8: normal
`super(id)` sets `this.id` before the `label` field initializer reads it. The second `new` call omits `priority`, so it uses the default value `"normal"`. Run the example in a JavaScript console that supports public class fields.
Related lessons
- JavaScript · Lesson 13 · Intermediate · Version: 3
- JavaScript · Lesson 38 · Intermediate · Version: 3