Notes: Object Oriented Programming (OOP) in Javascript, what need to know

Serhii Koziy
2 min readFeb 19, 2023

--

Object-oriented programming (OOP) is a popular paradigm for writing code in many languages, and JavaScript is no exception. OOP provides a way to structure code around objects, which can help to make code more modular, reusable, and easier to maintain. In this article, we’ll explore some of the key concepts of OOP in JavaScript.

  1. Classes

Classes are a fundamental part of OOP, and they provide a way to define objects with properties and methods. In JavaScript, classes are defined using the class keyword, which allows you to create a blueprint for creating new objects.

For example, you might define a class called Person that has properties like name, age, and gender, as well as methods like speak() and walk(). Once you've defined the class, you can create new instances of it using the new keyword.

2. Inheritance

Inheritance is another key concept in OOP, and it allows you to create new classes that inherit properties and methods from existing classes. In JavaScript, you can achieve inheritance using the extends keyword, which allows you to create a subclass that inherits from a parent class.

For example, you might define a subclass called Student that inherits from the Person class, but also has additional properties like grade and school. This allows you to reuse the code from the Person class while also adding new functionality specific to students.

3. Encapsulation

Encapsulation is a principle in OOP that involves hiding implementation details of an object and exposing only the necessary information and functionality to the outside world. In JavaScript, you can achieve encapsulation using private and protected properties and methods.

One way to implement encapsulation in JavaScript is by using closures to create private properties and methods that are only accessible from within the object. Another way is by using the WeakMap object to create private properties that are only accessible from within the object's methods.

4. Polymorphism

Polymorphism is the ability for objects of different classes to be treated as if they are the same type. In JavaScript, you can achieve polymorphism using interfaces or abstract classes, which define a set of properties and methods that must be implemented by any class that implements the interface or extends the abstract class.

Polymorphism can be useful for creating more flexible and generic code that can work with different types of objects. For example, you might define an interface called Drawable that defines a draw() method, and then implement this interface in different classes like Circle and Square.

In conclusion, OOP is a powerful paradigm for writing code in JavaScript, and it provides many benefits in terms of modularity, reusability, and maintainability. By understanding the key concepts of OOP, you can create more effective and efficient JavaScript code that is easier to understand and work with.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response