Is JavaScript a (true) OOP language?

I know, this topic has been discussed so many times, too many times. However it is always a current topic. Every time a Java or C# or any other OOP language developer get in touch with JavaScript, he complains of it. He says that working with it is a mess, that it has no types, it is not well-structured, it has several oddities, its object support is trivial and definitively it is not an OOP language.

Some of these complaints may be acceptable, but some others are prejudices, such as the claim that JavaScript has no types and that it is not an OOP language. Regarding the latter point, before affirming it you should ask yourself: what does make a programming language an Object-Oriented Programming language?

What is OOP?

The OOP paradigm is not based on a formal standard specification. There is not a technical document that defines what OOP is and what it is not. The OOP definition is mainly based on common sense taken from the papers published by early researchers as Kristen Nygaard, Alan Kays, William Cook, and others. There have been many attempts to define OOP and a widely accepted definition to classify a programming language as Object
Oriented is based on two requirements:

  • its capability to model a problem through objects
  • its support of a few principles that grant modularity and code reuse

In order to satisfy the first requirement, a language must enable a developer to describe the reality using objects and to define relationships among objects such as the following:

  • Association: This is the object’s capability to refer another independent object
  • Aggregation: This is the object’s capability to embed one or more independent objects
  • Composition: This is the object’s capability to embed one or more dependent objects

Commonly, the second requirement is satisfied if a language supports the following principles:

  • Encapsulation: This is the capability to concentrate into a single entity data and code that manipulates it, hiding its internal details
  • Inheritance: This is the mechanism by which an object acquires some or all features from one or more other objects
  • Polymorphism: This is the capability to process objects differently based on their data type or structure

Meeting these requirements is what usually allows us to classify a language as Object Oriented.

JavaScript and OOP

So, now we know how an OOP language should look like. Can we demonstrate that JavaScript is an OOP language? Let’s try.

Showing that JavaScript objects support Association, Aggregation and Composition is trivial. Consider the following code:

Here we have an example of how the three basic relationships can be implemented in JavaScript.

Regarding Encapsulation, JavaScript objects are entities supporting data and functions, but they haven’t an advanced native support to hide internal details. JavaScript objects do not care about privacy. All the properties and methods are publicly accessible if no caution is taken. However we can apply several techniques in order to define an object’s internal state and protect it from external access: from using getters and setters to exploiting closures.

Inheritance is supported by JavaScript in its basic level through the so-called Prototypal Inheritance. Even if some developer may consider it a bit rudimentary, JavaScript inheritance mechanism is fully effective and allows to get the same result as largely recognized OOP languages. Whatever you say, JavaScript has a mechanism “by which an object acquires some or all features from one or more other objects”, and this is inheritance.

With Polymorphism the challenge seems harder, because many associate this concept to data types. Actually plymorphism involves many aspects of a programming language, and it is not only related to OOP languages. Usually it involves items like generics, overloading and structural subtyping. All these things seem too much for a “simple” and weak typed language as JavaScript. This is not true: in JavaScript we can implement the different types of polymorhism in several ways and maybe we have done it many times unknowingly.

OOP without classes

OK, but JavaScript hasn’t classes.” Many developers do not consider JavaScript a true object-oriented language due to its lack of class concept and because it does not enforce compliance with OOP principles.
However, we can see that our informal definition make no explicit reference to classes. Features and principles are required for objects. Classes are not a real requirement, but they are sometimes a convenient way to abstract sets of objects with common properties. So, a language can be Object Oriented if it supports objects even without classes, as in JavaScript.
Moreover, the OOP principles required for a language are intended to be supported. They should not be mandatory in order to do programming in a language. The developer can choose to use constructs that allow him to create Object Oriented code or not. Many criticize JavaScript because developers can write code that breaches the OOP principles. But this is just a choice of the programmer, not a language constraint. It also happens with other programming languages, such as C++.
We can conclude that a lack of abstract classes and leaving the developer free to use, or not features that support OOP principles are not real obstacle to consider JavaScript an OOP language.

Andrea Chiarelli is the author of Mastering JavaScript Object Oriented Programming (Packt Publishing).