Is JavaScript an Object-oriented programming language?

Mohamed Kouache - December 4, 2023

post image

JavaScript is a versatile and dynamic programming language that supports multiple programming paradigms, including object-oriented programming (OOP). when it comes to JavaScript, OOP uses objects to organize and structure code. Here are some key concepts related to JavaScript and OOP:

Objects

  1. In JavaScript, everything is an object or can be treated as an object.
  2. Objects are instances of classes or prototypes and can have properties (attributes) and methods (functions).


Prototypes

  1. JavaScript uses a prototype-based inheritance model instead of the class-based model found in some other OOP languages.
  2. Objects can inherit properties and methods from other objects through their prototypes.


Constructor Functions

  1. Constructor functions are used to create objects. They are similar to classes in other languages.
  2. When a new object is created using a constructor function, it inherits properties and methods from its prototype.

function Person(name, age) {
  this.name = name;
  this.age = age;
}

// Creating an instance of the Person object
const person1 = new Person("John", 25);


Inheritance

  1. Objects in JavaScript can inherit properties and methods from other objects.
  2. Prototypal inheritance allows for a flexible and dynamic way of sharing functionality between objects.

function Student(name, age, grade) {
  // Call the parent constructor to inherit properties
  Person.call(this, name, age);
  this.grade = grade;
}

// Inherit methods from Person's prototype
Student.prototype = Object.create(Person.prototype);

const student1 = new Student("Alice", 20, "A");


Encapsulation

  1. Encapsulation is the concept of bundling data (properties) and methods that operate on that data into a single unit (object).
  2. In JavaScript, objects provide a way to encapsulate data and behavior.

function Car(make, model) {
  // Private variables
  let fuel = 100;

  // Public methods
  this.getFuel = function () {
    return fuel;
  };

  this.drive = function () {
    fuel -= 10;
    console.log("Driving...");
  };
}

const myCar = new Car("Toyota", "Camry");
myCar.drive();
console.log(myCar.getFuel()); // Output: 90


Polymorphism

  1. Polymorphism allows objects to be treated as instances of their parent class, providing a consistent interface.
  2. JavaScript supports polymorphism through its dynamic and loosely-typed nature.

function greet(person) {
  console.log("Hello, " + person.name);
}

const person = { name: "John" };
const student = { name: "Alice", grade: "A" };

greet(person); // Output: Hello, John
greet(student); // Output: Hello, Alice


Conclusion

JavaScript's implementation of OOP is unique and might differ from traditional class-based languages like Java or C++. It's important to understand the prototypal nature of inheritance and how it influences the design of object-oriented code in JavaScript.