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
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.