Javascript: hierarchy and inheritance in a prototypal paradigm

While seen by many as some messy little web page language, Javascript has a fairly unique elegance to it when put alongside other object-orientated programming languages (C#/Delphi/Python/Java/C++).  This uniqueness is part of the reason for the generally negative stigma towards the language. The prototypal inheritance model is unintuitive to those (such as myself) who have come from a classical inheritance background, and the weak dynamic typing allows beginners with little OOP knowledge to rapidly produce working but extremely messy, inconsistent, hackish code that reciprocally convinces them (and others) that Javascript is a messy, inconsistent, hackish language.

One only has to look at the booming node.js movement to see that this strange little language must have some positive side though.  To illustrate the object model and inheritance in Javascript, I put together a little script (on repl.it) to demonstrate the prototypal model by example (specifically, by emulating a traditional hierarchical paradigm), which may be viewed and run here.  When run, the script creates a list of Javascript built-in classes, in addition to some classes “derived” within the script.  A tree of these is then displayed in the console, showing the inheritance hierarchy:

Output

Output

Source

Leave a Reply