Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Class constructor and method definitions

Create instances with `new`, initialize their state in `constructor`, and distinguish prototype methods from static methods and `super()` from `super.method()`.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

class Base {
  constructor(value) { this.value = value; }
  method() { return this.value; }
  static method() { return new Base(0); }
}

class Derived extends Base {
  constructor(value) { super(value); }
  method() { return super.method(); }
}

Class constructor and method definitions separate instance initialization from behavior you can call later. new C(...) runs the constructor of C; in a base class, constructor(...) can initialize instance properties such as this.value. Calling C() without new throws TypeError. The special constructor is a plain, non-static method with the non-computed name constructor. Defining it twice is a SyntaxError, not an overload. Use default parameters to handle different argument lists in one constructor.

If you omit the constructor, JavaScript supplies a default: it does no extra work for a base class and forwards arguments to the parent constructor for a typical derived class. In an explicit derived constructor, call super(...) before using this; accessing this too early throws ReferenceError. A derived constructor can skip super(...) if it explicitly returns an object without using this.

In a class body, write method(...) { ... } without function or commas between methods. A public instance method lives on C.prototype and is shared by instances; a property such as this.value in the example belongs to one instance. static method(...) { ... } defines a method on the class itself: call it as C.method(). Ordinary public methods with the same name on the same object are not overloads: the later definition replaces the earlier one.

In obj.method(), this is obj. If you extract the method and call it as fn(), this is undefined because class bodies run in strict mode. Use obj.method.bind(obj) if you need to preserve the receiver. In a derived instance method, super.method() starts the lookup at the current prototype of the method’s home object (the object it was defined on, such as Derived.prototype) and follows the prototype chain. In the syntax shown, lookup starts at Base.prototype, but changing the prototype of Derived.prototype changes that starting point. It calls the method it finds with the current this; it does not call the constructor. This is ECMAScript syntax, not a DOM API. Check support in your target runtime.

Examples

{
  class Counter {
    constructor(value = 0) {
      this.value = value;
    }

    add(amount) {
      this.value += amount;
      return this.value;
    }
  }

  class StepCounter extends Counter {
    constructor(value, step) {
      super(value);
      this.step = step;
    }

    static defaultStep() {
      return 2;
    }

    advance() {
      return super.add(this.step);
    }
  }

  const first = new StepCounter(10, StepCounter.defaultStep());
  const second = new StepCounter(10, 5);
  console.log(first.advance());
  console.log(second.value);
  console.log(first.advance === second.advance);
}

Result

12
10
true

`super(value)` initializes the counter value, and `super.add(this.step)` increases the first instance's value to `12`. The second stays at `10`, and `true` shows that both instances share the same `advance` function.

Related lessons

  • JavaScript · Lesson 7 · Advanced · Version: 4
  • JavaScript · Lesson 17 · Advanced · Version: 4
  • JavaScript · Lesson 39 · Intermediate · Version: 3

Sources