A Guide To Prototype-Based Class Inheritance in Javascript: Creating A Logical Hierarchy of Objects
A Guide To Prototype-Based Class Inheritance in Javascript: Creating A Logical Hierarchy of Objects
Cat and Dog are inherited from Pet which is inherited from Animal.
A Dog and a Cat share similar traits. Instead of creating two different
classes,
we can simply create one class Pet and inherit Cat and Dog from it. But
the Pet class itself can also be inherited from the class Animal.
Before We Start
Trying to understand prototype is like crossing the river going from
coding to computer language design. Two completely different areas of
knowledge.
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 1/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
similar objects. You can also say a child object is ”derived” from its
parent.
Technically, this is what it looks like. Try not to think too much into
this. Just know that at the very top of hierarchy there is Object object.
There is nothing else above. That’s why it’s own prototype points to
null.
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 2/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Constructors
It’s impossible to understand prototype without understanding the
anatomy of constructor functions.
They do not point to the same object. Let’s break them down:
Let’s say we define a new class Crane (using either function or class
keyword.)
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 3/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Same exact thing happens when you define a class using class keyword:
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 4/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Now Crane object itself can be the ”prototype” of another object. And
that object can be prototype of another object. And so on. The chain
can go on forever.
Side Note: In the case of the ES5-style functions, the function itself is
the
constructor. But ES6 class keyword places constructor inside of its
scope. This
is just a syntactic difference.
Prototype-Based Inheritance
We should always use class and extends keywords to create and inherit
objects. But they are only candy wrapper for what actually goes on
behind the scenes.
Let’s define a new object Bird and add 3 properties: type, color and
eggs. Let’s also add 3 methods: fly, walk and lay_egg. Something all
birds can do:
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 5/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
You could have alternatively attached the lay egg method directly to
Bird.prototype as shown in the next example:
But this is not entirely true. I won’t go into the details as of just yet,
because I don’t fully understand the distinction here. But I plan on
updating this tutorial when I gather up some more insight on the
subject.
(Imagine de ning the same properties and methods on all of the children
objects individually all over again. It would take twice as much memory.)
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 6/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Let’s create several different types of birds. Even though all of them can
still fly, walk and lay_eggs (because they are inherited from main Bird
class,) each unique bird type will add its own methods unique to that
class. For example, only parrots can talk. And only raven can solve
puzzles. Only a songbird can sing.
Parrot
Let’s create a Parrot and inherit it from Bird:
The difference is that we call Bird’s constructor with Bird.call and pass
the Parrot’s this context, before attaching our own methods. Bird.call
simply adds all of its properties and methods to Parrot. In addition to
that, we are also adding our own method: talk.
Now parrot can fly, walk, lay eggs and talk! But we never had to
define fly walk and lay_eggs methods inside Parrot itself.
Raven
In the same way, let’s create Raven and inherit it from Bird:
Songbird
Now, let’s create Songbird and inherit it from Bird:
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 7/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Sparrow can fly, walk and lay eggs, because it was inherited from Bird
that defines all those methods.
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 8/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Note we must use super() which calls the constructor of the parent
class.
Overview
Class inheritance helps establish a hierarchy of objects.
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 9/11
07/06/2019 A Guide To Prototype-Based Class Inheritance In JavaScript
Sign up
https://medium.com/@js_tut/a-guide-to-prototype-based-class-inheritance-in-javascript-849d3c3ddca 10/11