Register to get access to free programming courses with interactive exercises

Template method JS: Dive into Classes

Late binding leads to one interesting consequence. You can call methods and properties defined in descendants from a base class, even if the descendants themselves may not exist yet. We call late binding “late” because the check only happens when we use the code.

This feature is used in the template method pattern when subclasses have a common logic that relies in part on the behavior of subclasses. We implement this logic in the method from the base class. The part that differs (for each subclass) is delegated to the descendants.

For example, let’s take our tags:

class HTMLElement {
  // Implementation of all methods of the parent class
  // https://hexlet.io/courses/js-classes/lessons/inheritance/theory_unit
}

Look at the toString() method. You can see that its code will remain identical for most tags. The only thing that changes is the name of the tag itself:

class HTMLAnchorElement extends HTMLElement {
  toString() {
    // Parent method
    const attrLine = this.stringifyAttributes();
    // Parent method
    const body = this.getTextContent();
    return `<a${attrLine}>${body}</a>`;
  }
}

We can modify the code so that the toString() method moves to the HTMLElement. And the only thing to the subclasses is the tag's name:

class HTMLElement {
  toString() {
    const attrLine = this.stringifyAttributes();
    const body = this.getTextContent();
    // `getTagName` is a method that all subclasses must implement
    const tagName = this.getTagName();
    return `<${tagName}${attrLine}>${body}</${tagName}>`;
  }
}

The resulting code is better than the original version, as it significantly reduces duplication (the tags are about 100 pieces!). But there's one catch. Tags are always single tags, so the current version of toString() won't work for them. This situation can be resolved in various ways, such as through inheritance.

Create two subclasses for the HTMLElement: one HTMLSingleElement and one HTMLPairElement. Now, specific tags classes must be inherited from one of the specified classes. Each of these classes will have its own implementation of the toString() method.

The resulting code is better than the original version. It significantly reduces duplication (the tags are about 100 pieces!). But there’s one catch. Tags are always single tags, so the current version of toString() won’t work for them. We can resolve the situation in various ways, such as through inheritance.

To resolve this issue, we can create two subclasses for HTMLElement:

  • HTMLSingleElement
  • HTMLPairElement

We should inherit specific tag classes from one of these specified classes. Each of these classes will have its implementation of the toString() method.

class HTMLSingleElement extends HTMLElement {
  toString() {
    const attrLine = this.stringifyAttributes();
    // `getTagName` is a method that all subclasses must implement
    const tagName = this.getTagName();
    // A single tag is created
    return `<${tagName}${attrLine}>`
  }
}

class HTMLPairElement extends HTMLElement {
  toString() {
    const attrLine = this.stringifyAttributes();
    const body = this.getTextContent();
    // `getTagName` is a method that all subclasses must implement
    const tagName = this.getTagName();
    return `<${tagName}${attrLine}>${body}</${tagName}>`;
  }
}

Despite the differences in the toString() implementation, both subclasses require their descendants to implement the same getTagName() method.


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

The Hexlet support team or other students will answer you.

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:

Bookmate
Health Samurai
Dualboot
ABBYY
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.