DEV Community

Cover image for Mastering Object-Oriented Programming in JavaScript: Best Practices and Examples
Yosi Leyid for Hasidic Development Group

Posted on

Mastering Object-Oriented Programming in JavaScript: Best Practices and Examples

Object-Oriented Programming (OOP) is a popular programming paradigm that allows developers to create modular, maintainable, and reusable code. JavaScript, a programming language that has become ubiquitous in web development, also supports OOP. In this blog post, we will explore OOP in JavaScript and how it can be used to write more organized, efficient, and scalable code.

What is Object-Oriented Programming?

Object-Oriented Programming is a programming paradigm that is based on the concept of objects. An object is a collection of data and behaviors that are defined by a set of properties and methods. OOP is centered around the idea that software systems are composed of objects that interact with each other to achieve a common goal.

OOP has four fundamental concepts that make it a powerful tool for software development: Encapsulation, Abstraction, Inheritance, and Polymorphism.

Encapsulation: Encapsulation is the process of hiding the internal details of an object and exposing only the necessary information to the outside world. This is achieved by defining the properties and methods of an object as public or private. Private members are only accessible within the object, while public members can be accessed from outside the object.

Abstraction: Abstraction is the process of simplifying complex systems by creating models that capture the essential features of the system while hiding unnecessary details. In OOP, this is achieved by defining abstract classes and interfaces that define the common properties and behaviors of a group of objects.

Inheritance: Inheritance is the process of creating new classes from existing classes. The new class inherits the properties and behaviors of the parent class, and can add new properties and behaviors or override existing ones.

Polymorphism: Polymorphism is the ability of objects to take on multiple forms or behaviors. In OOP, this is achieved by using interfaces and abstract classes to define the common properties and behaviors of a group of objects, while allowing each object to implement them in its own way.

JavaScript and OOP

JavaScript was originally designed as a scripting language for web pages. However, with the advent of Node.js, JavaScript has become a full-fledged programming language that can be used to create complex systems.

JavaScript supports OOP, but in a different way than other programming languages like Java or C++. In JavaScript, objects are created using a constructor function or an object literal.

Constructor Functions

A constructor function is a special function that is used to create new objects. It is called using the "new" keyword, which creates a new object and assigns it to the "this" keyword. The constructor function can define properties and methods on the object using the "this" keyword.

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

  this.greet = function() {
    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
  }
}

var john = new Person("John", 30);
john.greet(); // outputs "Hello, my name is John and I am 30 years old."
Enter fullscreen mode Exit fullscreen mode

In the above example, we define a constructor function called "Person" that takes two parameters, "name" and "age". We define two properties, "name" and "age", and a method called "greet" that logs a message to the console.

We create a new object called "john" using the "new" keyword and pass in the arguments "John" and 30. We then call the "greet" method on the "john" object.

Object Literals

Object literals are a shorthand notation for creating new objects. They are created using curly braces and can define properties and methods on the object.

var john = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
}
};

john.greet(); // outputs "Hello, my name is John and I am 30 years old."
Enter fullscreen mode Exit fullscreen mode

In this example, we define an object called "john" using object literal notation. We define two properties, "name" and "age", and a method called "greet" that logs a message to the console.

We then call the "greet" method on the "john" object.

Classes in JavaScript

In ES6, JavaScript introduced the class syntax, which provides a simpler and more structured way of defining objects and their behavior. Classes in JavaScript are still based on the prototype inheritance model, but they provide a more familiar syntax for developers who are used to classes in other programming languages.

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

  greet() {
    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
  }
}

var john = new Person("John", 30);
john.greet(); // outputs "Hello, my name is John and I am 30 years old."
Enter fullscreen mode Exit fullscreen mode

In this example, we define a class called "Person" that has a constructor function that takes two parameters, "name" and "age". We define two properties, "name" and "age", and a method called "greet" that logs a message to the console.

We create a new object called "john" using the "new" keyword and pass in the arguments "John" and 30. We then call the "greet" method on the "john" object.

Inheritance in JavaScript

Inheritance in JavaScript is achieved using the prototype chain. Each object in JavaScript has a prototype object, which it inherits properties and methods from. The prototype object, in turn, has a prototype object, and so on, until the top of the chain is reached, which is the Object prototype.

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

  greet() {
    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
  }
}

class Employee extends Person {
  constructor(name, age, salary) {
    super(name, age);
    this.salary = salary;
  }

  displaySalary() {
    console.log("My salary is " + this.salary);
  }
}

var john = new Employee("John", 30, 50000);
john.greet(); // outputs "Hello, my name is John and I am 30 years old."
john.displaySalary(); // outputs "My salary is 50000"
Enter fullscreen mode Exit fullscreen mode

In this example, we define a class called "Person" that has a constructor function that takes two parameters, "name" and "age". We define two properties, "name" and "age", and a method called "greet" that logs a message to the console.

We then define a class called "Employee" that extends the "Person" class. We add a new property called "salary" and a method called "displaySalary".

We create a new object called "john" using the "new" keyword and pass in the arguments "John", 30, and 50000. We then call the "greet()" method and the "displaySalary()" method on the "john" object.

The "Employee" class inherits the properties and methods of the "Person" class using the "extends" keyword. The "super" keyword is used to call the constructor of the parent class and initialize the "name" and "age" properties of the "Employee" object.

Conclusion

Object-oriented programming is an important paradigm in software development, and JavaScript provides various ways to implement OOP concepts. In this blog post, we covered the basics of OOP in JavaScript, including object literals, classes, and inheritance.

It's important to note that OOP is just one approach to programming, and there are other paradigms such as functional programming and procedural programming. Developers should choose the appropriate approach based on the problem they're trying to solve and the requirements of the project.

By understanding OOP concepts in JavaScript, developers can write code that is more organized, modular, and reusable. This can result in more maintainable code that is easier to debug and extend over time.

Resources

Object Cheat Sheet c/o Reddit:
9nxtqtho6q861

Top comments (0)