Advertisement
  1. Code
  2. JavaScript
Code

Simple JavaScript Inheritance: What You Need to Know

by
Difficulty:IntermediateLength:MediumLanguages:
Sponsored Content

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

A lot of my friends are C# or C++ developers. They are used to using inheritance in their projects, and when they want to learn or discover JavaScript, one of the first questions they ask is: “But how can I do inheritance with JavaScript?”

Actually, JavaScript uses a different approach than C# or C++ to create an object-oriented language. It is a prototype-based language. The concept of prototyping implies that behavior can be reused by cloning existing objects that serve as prototypes. Every object in JavaScript depends on a prototype which defines a set of functions and members that the object can use. There is no class—just objects. Every object can then be used as prototype for another object.

This concept is extremely flexible, and we can use it to simulate some concepts from OOP, like inheritance.

Implementing Inheritance

Let’s look at what we want to create with this hierarchy using JavaScript:

Inheritance in JavaScript

First of all, we can create ClassA easily. Because there are no explicit classes, we can define a set of behavior (A class so…) by just creating a function like this:

This “class” can be instantiated using the new keyword:

And to use it using our object:

Fairly simple, right?

The complete sample is just eight lines long:

Now let’s add a tool to create “inheritance” between classes. This tool will just have to do one single thing: clone the prototype.

This is exactly where the magic happens! By cloning the prototype, we transfer all members and functions to the new class.

So if we want to add a second class that will be child of the first one, we just have to use this code:

Then because ClassB inherited the print function from ClassA, the following code is working:

And produces the following output:

We can even override the print function for ClassB:

In this case, the produced output will look like this:

The trick here is to call ClassA.prototype to get the base print function. Then thanks to the call function we can call the base function on the current object (this).

Creating ClassC is now obvious:

And the output is:

More Hands-On With JavaScript

It might surprise you a bit, but Microsoft has a bunch of free learning on many open source JavaScript topics, and we’re on a mission to create a lot more with Microsoft Edge. Check out my own:

Or our team’s learning series:

And some free tools: Visual Studio Community, Azure Trial, and cross-browser testing tools for Mac, Linux, or Windows.

And Some Philosophy…

To conclude, I just want to clearly state that JavaScript is not C# or C++. It has its own philosophy. If you are a C++ or C# developer and you really want to embrace the full power of JavaScript, the best tip I can give you is: Do not try to replicate your language into JavaScript. There is no best or worst language. Just different philosophies!

This article is part of the web dev tech series from Microsoft. We’re excited to share Microsoft Edge and the new EdgeHTML rendering engine with you. Get free virtual machines or test remotely on your Mac, iOS, Android, or Windows device @ http://dev.modern.ie/.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.