Professional Innovations

November 21st 2016 / JavaScript

Is JavaScript Object Oriented?

Posted by ProInns

Let’s agree first that using object oriented techniques leads to code that is well structured, reusable and easy to manage and maintain.

The main question that we aim to answer in this article is the following: is JavaScript an object oriented language? Answering that question depends on how we determine whether a specific programming language is object oriented or not. Different people have different opinions so I will try to present my own point of view and based on it answer the question.

For a programming language to be object oriented, it has to enable the developer to define types and instantiate them. Encapsulation and data hiding are basic concepts that must be available in addition to other features such as shared members, inheritance, overloading, overriding and polymorphism. If the language enables the developer to implement the above mentioned concepts, we can consider it as object oriented.

The above definition does not dictate how the concepts are supported by the language; we just require them to be supported in a way or another; implementing them is the sole responsibility of the developer.

A more strict definition might go further and dictates that for a language to be object oriented, the concepts mentioned above need to be part of the language constructs. We need terms like classes and interfaces to be part of the language constructs; data encapsulation to be supported through access modifiers that are part of the language itself and polymorphism to be available in the most intuitive manner. Structured exception handling must be fully supported by the language.

Let’s use those two definitions, the first with relaxed rules and the second that is stricter to find out if JavaScript is actually object oriented.

Inside JavaScript, we can create types and instantiate them by invoking their constructors. Encapsulation is partially supported; for instance, we can have private members but not the “private” access modifier; we use the concept of closures to define private members for a type; we cannot define “friend” members nor “protected” ones; shared members are there but not the “shared” keyword; the whole concept of overloading is absent due to the way functions are defined in JavaScript (functions are objects themselves; more on that in further articles). The concept of inheritance is supported but through what we call “prototype chaining”; we have to apply tricks in order to implement that simple concept which can be implemented with a single keyword in languages like C# (: parent class’ name) or Java (extends). JavaScript itself is not a typed language; a variable can hold values of any type; you can define a variable and place a string inside it; later on you can freely change it to any other type including a reference to an object; hence, the whole concept of polymorphism is far from being available in the way we know it in strictly typed languages such as C#, Visual Basic or Java.

If we try to go further and apply the more strict rules of our second definition, JavaScript does not recognize terms likes classes or interfaces; trivial object oriented keywords such as shared, overloads, overrides etc. are completely missing; the only shining point is that structured exception handling is supported to a great extent.

We conclude by saying that NO, JavaScript is not object oriented; although we can write object oriented code (at the end, this is how front-end controls are written), this will highly depend on the developer’s skills with a very limited support from the language itself (further on how to write object oriented code in JavaScript in further articles). One might wonder at the end, do we really need to write object oriented code when we program in JavaScript. Isn’t it a client-side language designed to work within the context of a browser on the user’s machine? Well to answer this question, we have to go back to the first couple of lines that preluded this article.