Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
class Animal { constructor(name, sound) { this.name = name; this.sound = sound; this.alive = true; } speak() { return this.name + ' says ' + this.sound; } describe() { return 'I am ' + this.name; } toString() { return 'Animal(' + this.name + ')'; } } const cat = new Animal('Cat', 'meow'); const dog = new Animal('Dog', 'woof'); console.log(cat.speak()); // Cat says meow console.log(dog.speak()); // Dog says woof console.log(cat instanceof Animal); // true console.log(cat.toString()); // Animal(Cat) console.log(cat.alive); // true
Result
Open