OOP in JavaScript vs other languages

This lesson introduces OOP in JavaScript and compares it to other languages by going over the advantages/disadvantages of both.

JavaScript has relied on the object-based style of programming since the beginning as functions and methods in it can be found written in this style.

However, OOP in JavaScript works differently from other languages. Hence, if you are familiar with OOP in other languages it is important you put that knowledge aside for now since holding on to those concepts might confuse you.

Now, let’s look into why OOP in JavaScript differs from other languages.

OOP in Other languages

You must have seen that other languages, such as C++, Java, and C#, use the keyword class to define a class. A class has properties and methods in it for every instance of that class.

Note: If you try to remove the class keyword the code will not compile and will generate an error.

The class, in this case, acts as a blueprint for the object or you can say that the object will be an instance of this class.

OOP in JavaScript

We can implement OOP without using classes as well, and that is what JavaScript does. Before and even after introducing its ES2015 version, JavaScript still relied on Prototype-based programming. In this programming style, the object encapsulates the properties, i.e., it’s methods and data, instead of a class. You can add new properties to this object whenever you feel like. So now, an object can be an individual instead of being an instance of the class, meaning if you want an object you easily just create one without having to create a class first.

Which Is Better?

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy