What is the output of the following code? class Base { constructor() { this.init(); } init() { this.value = ‘base’; } } class Derived extends Base { init() { this.value = ‘derived’; } } const d = new Derived(); console.log(d.value);

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? class Base { constructor() { this.init(); } init() { this.value = ‘base’; } } class Derived extends Base { init() { this.value = ‘derived’; } } const d = new Derived(); console.log(d.value);

Key points

  • Virtual dispatch allows the Derived class method to be called instead of the Base class method.
  • This behavior occurs even though the Derived class is not fully initialized yet.
  • JavaScript uses dynamic dispatch to determine the correct method to call at runtime.

Ready to go further?

Related questions