Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Is JavaScript a (True) OOP Language?

DZone's Guide to

Is JavaScript a (True) OOP Language?

To be object-oriented or not to be object-oriented, that is the question. But, really, that's the question we take a look at in this article.

· Web Dev Zone
Free Resource

Should you build your own web experimentation solution? Download this whitepaper by Optimizely to find out.

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 gets in touch with JavaScript, he complains about 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 it is definitively 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 to 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 on a single entity of data and the 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 what 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:

var johnSmith = {
 firstName: "John",
 lastName: "Smith",
 address: { //Composition
 street: "123 Duncannon Street",
 city: "London",
 country: "United Kingdom"
 }
};

var nickSmith = {
 firstName: "Nick",
 lastName: "Smith",
 address: { //Composition
 street: "321 Oxford Street",
 city: "London",
 country: "United Kingdom"
 }
};

johnSmith.parent = nickSmith; //Association

var company = {
 name: "ACME Inc.",
 employees: []
};

//Aggregation
company.employees.push(johnSmith);
company.employees.push(nickSmith);

Here you will find an example of Composition (the address property), an example of Association (the parent property) and an example of Aggregation (the employees property).

Regarding Encapsulation, JavaScript objects are entities supporting data and functions, but they don't have 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 developers consider it a bit rudimentary, JavaScript's inheritance mechanism is fully effective and allows you to get the same results as with 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, polymorphism 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 polymorphism in several ways and maybe we have done it many times unknowingly.

OOP Without Classes

"OK, but JavaScript doesn't have 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 makes 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 use, features that support OOP principles are not real obstacles to considering JavaScript an OOP language.

Implementing an Experimentation Solution: Choosing whether to build or buy?

Topics:
javascript ,object-oriented programming ,encapsulation ,polymorphism ,web dev

Published at DZone with permission of Andrea Chiarelli. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}