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. And the descendants themselves may not even exist. Late binding is late because the check only happens when this code is in use.

This feature is used in the template method pattern. It applies when subclasses have a common logic that relies in part on the behavior of subclasses. This logic is implemented in the method from the base class, and the part that differs (for each subclass) is delegated to the descendants.

Let's take our tags as an example.

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 that will be left to the subclasses is the name of the tag.

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.

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 implementation of toString(), both of these subclasses require their descendants to implement the same getTagName() method.


Hexlet Experts

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

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

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.