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?
Asked
Active
Viewed 133 times
-6
-
[JSbin](https://jsbin.com/yapogiqena/edit?js,console) – Zoltan Toth Mar 15 '19 at 18:41
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes – ponury-kostek Mar 15 '19 at 18:42
-
Check this link for [ES6 class definition](https://googlechrome.github.io/samples/classes-es6/). – k3llydev Mar 15 '19 at 18:42
1 Answers
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