bhylu.com

Advertisements

JS Class Inheritance

Extends creates a subclass. A derived constructor must call super before accessing this.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

class Member {
  constructor(name) { this.name = name; }
}
class Volunteer extends Member {
  label() { return this.name + " helps today"; }
}
console.log(new Volunteer("Mira").label());

What happens

The subclass reuses the parent constructor and adds a method.