-6

I have a class "dog" and want its prototype to be a new class, animal. How do I create this in JS using ES6 syntax?

alexcode
  • 1
  • 2

1 Answers1

3

A class in ES6 is defined as it follows. You mentioned Dog, so let's create a class named Dog:

class Dog{
    constructor(breed, age){
        this.breed = breed;
        this.age = age;
    }
    // Method to make the dog jump 
    jump (){
        // Make it jump in some way
    }
}

So, that's how a class is defined in ES6. The things that may "return" some information are called "methods". In this case, jump() is a method. If you want it to return something you just write return <some-object/value>; in the method's last line.

If prior to Dog, you have an Animal class, you can make your Dog class to extend the Animal class as follows:

class Dog extends Animal

This is called "inheritance" in programming. So your Dog class/object will inherit all methods avaialable in the class Animal.

k3llydev
  • 2,057
  • 2
  • 11
  • 21
MarkSkayff
  • 1,334
  • 9
  • 14