Javascript Instances and classes

If you create a class that extends another class and you call super in your constructor, does this create 2 instances (one super class and one sub class).?

Im trying to understand where it all lives eg;
If you create the super class Animal, and then a sub class Cat which extends animal, when you run the code, are 2 instances created, such as an Animal instance, and a Cat instance both in memory. And if your using one of the methods from the Super class, are theses methods created in the subclass or is there just some kind of link to the super class.

I have no issues with using them, but its just bugging me because i dont know lol

Hey @jayfromwales

You aren’t talking about a constructor function but a constructor like this, right?

class Animal {
    constructor(parameter) {

That doesn’t create an instance at all, it just creates the class. You create an instance of animal with const animal = new Animal(arg);.
If you write a class like Cat which extends your class Animal and call super in the Cat’s constructor, your Cat class inherits all the properties from the Animal class. And when you create an instance of Cat with const cat = new Cat(arg); it has all the properties from your Animal and your Cat class.

Hi @mirja_t
Thanks for replying :slight_smile:

I understand now, i was thinking that creating the classes were the instances, but they are just templates for creating new instances of them.

So can i ask, when we create a class, where does it get stored, is it just in memory until the program finishes?
Also, if we create an instane of Cat, it gets all its properties and methods from the Cat Class, but whilst this Cat instance is created is it in turn pulling info from the Animal class, or is all the Animal Class infor stored in the Cat class when we create it?



Hi Jamil,

Yes, that’s right.

I don’t really understand the question as you don’t necessarily have one monolithic program that runs through from top to bottom, but a js file that contains different functions, classes, objects, which are available to your ‘program’ in which you’d have to either call them in case of a function or create an object instance with your class.

When you create a class (Cat) which extends another class (Animal), the Cat class inherits the properties and methods from the Animal class before you create an instance of it. So the instance doesn’t ‘pull’ anything retroactively. When you use the super keyword in your Cat constructor, you merge Cat specific properties which you define in your Cat’s class constructor with Animal’s properties from the Animal’s class constructor. Otherwise the Cat class would just have the same properties as the Animal class.