Inheritance in Classes
Com a keyword
extendsdefinimos qual vai ser a classe que vai ser a pai para herdarmos:
class TaxedProduct extends Product {
Com o keyword
super()fazemos o uso do construtor da classe que estamos herdando:
constructor(name, price, taxRate = 1.2) {
super(name, price);
this.taxRate = taxRate;
}
Exemplo:
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
toString() {
return `toString: Name: ${this.name}, Price: ${this.price}`;
}
}
class TaxedProduct extends Product {
constructor(name, price, taxRate = 1.2) {
super(name, price);
this.taxRate = taxRate;
}
getPriceIncTax() {
return Number(this.price) * this.taxRate;
}
toString() {
let chainResult = super.toString();
return `${chainResult}, Tax: ${this.getPriceIncTax()}`;
}
}
let hat = new TaxedProduct("Hat", 100);
let boots = new TaxedProduct("Boots", 100, 1.3);
console.log(hat.toString());
console.log(boots.toString());
05 November 2025