Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 785 Bytes

how_do_you_extend_classes.md

File metadata and controls

29 lines (23 loc) · 785 Bytes

How do you extend classes?

In JavaScript, you can extend a class using the extends keyword. This allows a subclass to inherit properties and methods from a parent class.

Example:

class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(`${this.name} makes a sound`);
  }
}
class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks`);
  }
}
const dog = new Dog('Buddy');
dog.speak(); // 'Buddy barks'

Tags: intermediate, JavaScript, classes

URL: https://www.tiktok.com/@jsmentoring/photo/7466566601702133024