Is Java Script Really An Object Oriented Language?

There have been a lot of debate over whether or not JavaScript is an Object Oriented Programming language.

After reading a lot of material on this question I have a short answer for you - YES, JavaScript is an OOP language!

Especially when JS-ninjas who are known for their contributions to the language itself, like Douglas Crockford say it, you might want to believe it.

But, it is not a class-based object-oriented language like Java, C++, C#, etc.

Let's go a little deeper into why JavaScript might be an OOP language. And, explore the basics of object-oriented programming (OOP) using JavaScript.

OOP - Basics

So, what makes any programming language Object Oriented? Well, there are various definitions on this but essentially, for any language to be Object Oriented language it has to support following important features at the least:

1. Encapsulation - Object's internal working is hidden from the rest of the application.

2. Inheritance - Object can inherit properties and method from a parent object.

3. Polymorphism - Object can having multiple forms.

So, if we can prove that JavaScript supports all of the above features we can call it an Object Oriented Programming language. Let's see each feature one by one in action using JavaScript.

Creating Objects in JavaScript

In JavaScript, we can easily create objects out of thin air.

For example, let's say we are creating a game and we want to create a player object. We can define the player's name as "Duffy" and the number of lives as 3.

This data is stored in an object, which is essentially a dictionary with keys and values.

const player = {
name: "Duffy",
lives: 3,
};

Tip: We can even represent this object as a JSON string and pass it over an HTTP request.

But objects in JavaScript can also have functions as values. Isn't that cool?

We can add a function called greet to the player object, which will log "Hello" to the console when executed.

const player = {
name: "Duffy",
lives: 3,
greet: function() {
console.log("Hello");
},
};

By executing player.greet(), we can see the output "Hello" in the console.

Encapsulation.

Encapsulation simply means, private data of an Object should not be directly accessible to the outside world. There should be methods to get and to set the internal properties of that object.

First, let's see, what is NOT encapsulation?

var Person = function (){
return {
fullName: "John Doe"
};
};
var user = Person();
console.log(user.fullName); // "John Doe"
user.fullName = "Foo Joe"

Although it is perfectly legal to write the above code, it is not a recommended way to write it that way due to various reasons like the security of the code, its lack of ability to validate the code, and much more.

What Is Encapsulation?

var Person = function (){
var fullName = "John Doe";
return {
getName: function(){
return fullName;
},
setName: function(name){
fullName = name;
}
};
};
var user = Person();
console.log(user.getName()); // "John Doe"
user.setName("Foo Joe");

The above code is encapsulated because the property fullName cannot be accessed from anywhere outside the Person function.

Thus, by the definition of encapsulation we saw above, it is proved that JavaScript supports encapsulation.

Inheritance

This feature gives the ability to an object to inherit functionality and properties from parent objects.

Developers coming from C++/Java background- please be advised that JavaScript supports prototypal inheritance and not the usual classical inheritance.

I will cover what are prototypes and what is prototypal inheritance in future post. But, for now, it simply means that each object in JavaScript serves as a prototype which can be cloned to create another object having similar behavior as the original object in question.

Now, let's look at a very simple example demonstrating prototypal inheritance in JavaScript.

var animal = { sleeps: true };
var tiger = Object.create(animal);
tiger.eats = true;
console.log(tiger.sleeps); // true
console.log(tiger.eats); // true

In the above example, Object.create() is used to create a new object by specifying its prototype object. A "tiger" object was created using "animal" as its prototype. And due to prototypal inheritance, "tiger" object also got "sleeps" property from its parent "animal" object.

Thus, by the definition of prototypal inheritance, it is proved that JavaScript supports inheritance.

Polymorphism.

JavaScript being a dynamic language by nature, it does not support all the classical types of polymorphism like function overloading or parametric based polymorphism. But, based on the kind of inheritance it exhibits, JavaScript supports subtype polymorphism which is very popular in even class-based languages like Java/C++.

Continuing the same example we saw in inheritance, let's see how polymorphism can be implemented in JavaScript.

var animal = {
sleeps: true,
getInfo: function(){
return "I am animal";
}
};

"animal" is a base object. Let's inherit "lion" and "tiger" objects from "animal".

var lion = Object.create(animal);
var tiger = Object.create(animal);
tiger.getInfo = function(){
return "I am tiger";
};

We used Object.create() method to inherit "lion" and "tiger" objects from "animal". Then for the "tiger" object we overrode the getInfo function to be more specific to that object. But, we did not do the same for the "lion" object.

Now, let's see what happens when getInfo function is called on both the objects.

console.log(lion.getInfo()); // "I am animal"
console.log(tiger.getInfo()); // "I am tiger"

As we did not override getInfo function for "lion" object it uses the getInfo function of its parent, that is, "animal" object. On the other hand, we wrote a specific getInfo function for the "tiger" object hence when the getInfo was executed on it, it executed the specific function to that object instead of the getInfo function of its parent Animal object.

The getInfo function is resolved at runtime, and this is called late binding or dynamic Polymorphism.

Thus, it is proved that JavaScript supports polymorphism.

Final Thoughts

Based on above examples, we can definitely say that, although different from class-based languages, JavaScript is also an Object Oriented Programming language. Remember, everything in Javascript is an object, compared to C++ or Java.


Further Resources We Recommend


Try Etsy For Free
Previous: How To Sell Digital Downloads On Etsy? Complete GuideNext: AngularJS - How To Customize Kendo Multi-Select Dropdown Widget?

Share This Post