Prototype-based vs Class-based Object Oriented Programming for Dummies

As a beginner to Javascript I was introduced to the concept of Object Oriented programming. Part of that introduction was the idea that different languages implement OOP in different ways. While many of the languages have their own spin on it, in general it can be split into 2 main types.

  1. Class-based
  2. Prototype-based

…And that was about as far as I got.

The lesson went on to explain the differences between the 2 types and the pro’s and con’s of each one, but the more I read the more and more lost I became. The whole concept of OOP was relatively new to me, so all of the technical language being used to describe the differences flew completely over my head.

I decided to spend some time and do some reading on the topic, until I came to some sort of an understanding of the concepts being discussed. The following is my attempt to explain the difference between these 2 forms of Object Oriented Programming as I understand them, using as little technical terminology as possible, so that somebody who is in the same position that I was in might have a better understanding from which to start his OOP journey.

To use an analogy, imagine you have 2 different 3D printers, that could both print cars. Both printers comes with a basic template with which you could print an extremely basic but fully functional car.

3D Printer #1 uses templates. If you want to print a car you take your disc with your template and print your first car. Congratulations!

Now let’s say you are tired of manually rolling up your windows and you want to upgrade your car to have automatic windows. This car is not capable of upgrading itself, the only way to upgrade is by creating a new template and printing a new car. Luckily you don’t have to create your new car from scratch you can simply make a copy of your basic car template onto a new disc and on your new disc change the window settings to be automatic instead of manually. Now go ahead and print your new car with automatic windows.

Wow that was easy, wasn’t it? Enter Class-based OOP.

With classes you can make stand-alone self dependent “objects” of code that have built into them everything that is needed for the functionality of a specific object. For example our “basic car” template has the settings for turning on the engine, turning off the engine, drive forward, drive in reverse etc. If you want to create a car all you need to do is use this template and “print” a fully functional car. Or, if you want want to upgrade your car, you can copy this template to get the basic car functionality and tweak it to get your upgrade. Now to take this one step further, let’s say in addition to your automatic windows you now also want heated seats. You can simply take the second template you made of your “upgraded car” and create a copy of that and just add heated seats. Just like that you can print a brand new car with automatic windows and heated seats!

Now to our 3D Printer #2. Our second printer isn’t just a printer, this printer itself comes as part of a car! You can turn the car’s engine on or off, you could even take it for a test drive if you’d like. But the greatest thing about this car is that it comes with a 3D printer, that can print cars exactly like it, even with their own 3D printer!

Now let’s say the owner of this car, wants to have cruise control. No need to scrap your current car and make a new template to print a whole new car. This car you could simply bring into the shop and ask them to add all the latest gadgets. Once you do that your 3D printer also changes and now it prints a whole new souped up car just like this one.

But what if you don’t want to change your current car. You’re an old timer and you like the feel of your foot on the gas pedal the whole drive. But your son just turned 18 and it’s time to print a new car for him to drive. Your son of course wants all the latest features, so what do you do? With your 3D printer you can print a new basic car and bring that new car into the shop to give it all the upgrades. Now you have a new fancy car, and this fancy car, not only can your son show off all the fantastic features to his friends, but he can even print for them their own fancy car with his car’s own 3D printer.

This second 3D printer is a whole different animal. Just to highlight a few key differences.

  1. Printer #1 doesn’t drive, it’s a blueprint, it can only print cars that drive. Printer #2 is part of a car that can drive on its own.
  2. The cars you print with Printer #1 are an exact copy of the template, you can’t upgrade once it’s printed. In order to upgrade you need a new template to print a new car. Whereas with Printer #2 you can modify your printed car to have whatever features you’d like, and that car will now be able to print cars with these new features.

To bring the analogy full circle:

Class-based object oriented programming is about creating a blueprint. It itself cannot be used (invoked). Its function is purely to create an objects(s) with the exact specifications described in it. To create an object that is even slightly different you must create a new template.

Prototype-based object oriented programming is about creating a prototype which itself is a fully functional “object” or function. Its power is in its ability to be used as prototype from which you can create identical objects or functions that do the exact same thing, only with a few differences which you can then go on to specify.

I understand that isn’t an exact analogy and obviously this doesn’t come close to explaining all the differences between these 2 forms of programming. But my purpose is only to explain in very simple terms the general difference between these 2 ideas, and the reader is encouraged to now go find an article that explains them in greater detail with all the pro’s and con’s between them.