Register to get access to free programming courses with interactive exercises

Inheritance JS: Dive into Classes

Class inheritance allows the creation of subclasses based on base or superclasses, where subclasses inherit the structure of the base classes. In JavaScript, classes are essentially prototypes. Inheritance will be studied in stages throughout the course due to its complexity.

To illustrate inheritance, consider the HTML structure where each HTML tag has unique characteristics while sharing common attributes:

// Base class for all tags
// It knows how to work with attributes
class HTMLElement {
  constructor(attributes = {}) {
    this.attributes = attributes;
  }

  setAttribute(key, value) {
    this.attributes[key] = value;
  }

  getAttribute(key) {
    return this.attributes[key];
  }

  getTextContent() {
    return this.body;
  }

  setTextContent(body) {
    this.body = body;
  }

  stringifyAttributes() {
    // build: key="value" key2="value2"
  }
}

Specific elements represented by tags in HTML inherit this class:

// Anchor is a link. The `HTMLAnchorElement` class defines the tag `a`
// We do inheritance using the keyword `extend`
class HTMLAnchorElement extends HTMLElement {
  toString() {
    // Parent method
    const attrLine = this.stringifyAttributes();
    // Parent method
    const body = this.getTextContent();
    return `<a${attrLine}>${body}</a>`;
  }
}

We represent inheritance as "A extends B" indicating that A inherits B. Now let's see how inheritance works:

// The parent constructor
const anchor = new HTMLAnchorElement({ href: 'https://ru.hexlet.io' });
anchor.setTextContent('Hexlet');
console.log(`Anchor: ${anchor}`); // `toString() `is called automatically
// => Anchor: <a href="https://hexlet.io">Hexlet</a>

HTMLAnchorElement does not have a constructor definition but has access to all the properties of the superclass through inheritance. Methods not available in the current class, such as toString(), are automatically called from the parent class.

Chain of Inheritance

Unlike interfaces, JavaScript allows single-class inheritance.

It means we can inherit only one class to avoid problems related to multiple inheritances, such as method and property collisions. However, the chain of inheritance can be as deep as desired:

class D {}
class C extends D {}
class B extends C {}
class A extends B {}

Type check operator

The instanceof operator considers classes from the prototype inheritance chain:

const anchor = new HTMLAnchorElement();
if (anchor instanceof HTMLElement) {
    console.log('!!!');
}
// => !!!

Don't forget that such checks mean you can't use polymorphism. Sometimes you can't do without them, but in the vast majority of cases it's better to be bound to the object interface.


Recommended materials

  1. Inheritance (Wiki)

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.