What does the following code output? class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a noise.`; } } class Dog extends Animal { speak() { return `${this.name} barks.`; } } const d = new Dog(‘Rex’); console.log(d.speak());

JavaScript Developer Medium

JavaScript Developer — Medium

What does the following code output? class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a noise.`; } } class Dog extends Animal { speak() { return `${this.name} barks.`; } } const d = new Dog(‘Rex’); console.log(d.speak());

Key points

  • Dog class overrides the speak method from the Animal class
  • The new Dog instance 'd' is created with the name 'Rex'
  • The speak method of 'd' outputs 'Rex barks.'

Ready to go further?

Related questions