Skip to main content

Classes

caution

Before reading this please note that classes are in an experimental stage in Hedgehog Script and don't support certain things.

Classes

Classes are a template for creating objects.

Their syntax is like functions - define class expressions and give a class declaration.

The purpose of classes is to create an abstraction of certain objects.

Then one can use the template to repeatedly create new objects of that type.

An example: cars are vehicles - which are machines - a Honda is a car type, specifically a 2001 Honda Civic.

  • Can you define the subclasses and superclasses?
info

Classes are just Objects under the hood in Hedgehog Script.

Let's view an example of declaring a class with a constructor:

A Class can have both subclasses and superclasses. Hence, one can effectively have inheritance.

caution

A ReferenceError will be thrown if a class is constructed or initialized before it's declared. (unlike functions)

The way we defined the class above is via class expressions.

There is another way, direct declaration:

tip

As shown above one can do a few important things in the class body:

  • Define methods/properties and define the constructor

 

info

An important concept is binding this. Check out this to learn about this.

Regarding subclasses, creating them as a derived class is as simple as adding extends:

  • class [subClassName] extends [className] {constructor(...) { super(...) } }

     

info

One can override class body methods: Consider 'Animal' has a subclass 'Hippo', which has a different version of eat().

  • Instances of 'Hippo' will use their eat() instead of Animal.eat().

 

tip

For more, detailed information, visit: Classes - MDN