CKTIS / JavaScript
Class constructor and method syntax
A constructor initializes the object created by `new`, instance methods are shared through the prototype, and static methods belong to the class.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
class Base {
constructor(value) { this.value = value; }
read() { return this.value; }
static kind() { return "base"; }
}
class Derived extends Base {
constructor(value) { super(value); }
read() { return super.read(); }
}
const instance = new Derived(5);
instance.read();
Derived.kind();Class constructor and method syntax uses constructor(...) { ... } and method(...) { ... } inside class, with no function keyword or commas between definitions. new C(...) runs the constructor; calling C(...) without new throws TypeError. A second special constructor in the same class is a SyntaxError, not an overload. Use default parameters or branching in one constructor instead. static constructor() and ['constructor']() are ordinary methods, not special constructors.
A public instance method lives on C.prototype and is shared by instances. A public static method belongs to C; call it as C.method(), not through an instance. In obj.method(), obj is the receiver and becomes this. Extracting the method does not bind the receiver: after const f = obj.method, a bare f() call has this set to undefined.
Ordinary method definitions can use forms such as async method(), *method(), and async *method(). Accessors get value() and set value(v) run on property reads and writes, without a call with parentheses. An instance field method = () => {} creates a separate function per instance, not a shared prototype method. Multiple ordinary public methods with the same name on the same object do not overload by parameter list: the last definition replaces the previous one.
If you omit the constructor, a base class gets an empty default constructor. A derived class’s default constructor forwards its arguments to the superclass constructor if it is constructible. A class can be defined with extends null and no explicit constructor, but creating an instance with new throws a TypeError because there is no constructible superclass to receive the arguments. In an explicit derived constructor, call super(...) before accessing this or you get a ReferenceError. You can skip super(...) by explicitly returning a different object without accessing this. By contrast, super.method() starts method lookup at the parent in the prototype chain while keeping the current this.
This is ECMAScript 2024 language syntax, not a browser API. Check support for the individual syntax forms in your target runtime.
Examples
{
class Counter {
constructor(value) {
this.value = value;
}
read() {
return this.value;
}
static kind() {
return "counter";
}
}
class StepCounter extends Counter {
constructor(value, step) {
super(value);
this.step = step;
}
read() {
return super.read() + this.step;
}
}
const a = new StepCounter(5, 2);
const b = new StepCounter(8, 3);
console.log(a.read());
console.log(b.read());
console.log(a.read === b.read);
console.log(StepCounter.kind());
}Result
7 11 true counter
`super(value)` initializes `value`, and `read()` adds `step` to the base method’s result. `true` confirms that both instances share the same `read` function. `StepCounter.kind()` calls the inherited static method.
Related lessons
- JavaScript · Lesson 26 · Advanced · Version: 4