With the addition of the
However, it should be noted that this is nothing more than syntactic sugar over an extremely expressive and flexible language construct.
In fact it is very easy to build our own Class implementation, using specially constructed ‘class’ functions as templates for the ‘new’ instances we wish to use.
Looking at the example above you will notice that we take into consideration the possibility of either a new class being defined (one parameter) or an inherited class being supplied.
The new ‘class functions’ prototype is composed of its parents, along with any new properties supplied.
The constructed function which is returned will be invoked as a constructor function throughout its lifetime, creating new instances of the defined class.
We also include a simple
super function which allows the user to call its parents method implementation.
We are now able to use this implementation in a contrived representation of both a User and Admin class.
You will notice how we call the parent classes (User)
getName implementation within the Admin class.
We are then able to create a couple of ‘class instances’ and type-check them accordingly.