inheritance

JavaScript Inheritance via Prototypes and Closures

JavaScript as a prototypical language offers loads of solutions to implement classes and class inheritance. When coming from an Object Oriented language such as Java or Ruby, these solutions aren't obvious at the first glance.

I started coding JavaScript years ago and always found a solution for my specific case. But things felt messy and I always thought "Is this really the right way of doing it?". Well, after all those years, experience took away this feeling and now I want to share some stuff that finally meant a break-through of understanding for me.

There are two different concepts of doing inheritance in JavaScript. One is using the prototype property of a function as a mean to create something similar to a "class". The other concept is using the constructor function itself to create a class dynamically during runtime.

For both concepts I'll show you their basic idea and compact helpers for creating inheritance in an elegant way.

Let's start with the prototype-based concept.

Syndicate content