StepsEasy
JS Class Inheritance - Example
Run
Reset
Destroy & Close
← Back to lesson
Code Editor
javascript
class Animal { constructor(name, sound) { this.name = name; this.sound = sound; } speak() { return `${this.name} says ${this.sound}`; } } class Dog extends Animal { constructor(name) { super(name, 'Woof!'); } fetch(item) { return `${this.name} fetches the ${item}`; } } const dog = new Dog('Rex'); console.log(dog.speak()); // Rex says Woof! console.log(dog.fetch('ball')); // Rex fetches the ball
Result
Click "Run" to execute the code...
Loading runtime...
Ready
JAVASCRIPT | In-Browser Execution