Sreejit's Tech Blog

Inheritance in JavaScript: A brief overview
Sreejit De

Sreejit De

Jan 25, 2022

Inheritance in JavaScript: A brief overview

Why is Inheritance useful?

Let's say two objects share a common property, and both objects are of similar type: ⁠

snippet1.png

As we can see, both the constructors Bus & Bike share a common printWheelCount() property, so we are writing the same code twice by including it in both prototypes separately. There's a way to make this code reusable, and that is where inheritance comes in. Since the classes Bus Bike are of similar types, we can make a super class Vehicle, and add the common property to the super class. Then, we can inherit the properties of Vehicle inside Bus & Bike. This is how inheritance helps us in following the DRY principle in programming.

Sreejit De

Sreejit De

Software Engineer who is passionate about building things for the web

Add a comment

      Related Posts

      No related posts found!

      Categories