DEV Community

Cover image for Understanding Object-Oriented Programming (OOP) in JavaScript

Understanding Object-Oriented Programming (OOP) in JavaScript

henry messiah tmt on September 13, 2025

Introduction Programming is much like building a city. You don’t throw bricks, cement, and metal randomly together, instead you carefull...
Collapse
 
adika_david_f4882f8904c91 profile image
ADIKA DAVID

Great write-up, Henry! The real-world analogies (especially the car system example) made the OOP concepts much clearer and relatable. I also like how you broke down encapsulation, abstraction, inheritance, and polymorphism in a beginner-friendly way. This will definitely help a lot of learners approach JavaScript OOP with more confidence. Looking forward to more tutorials from you. Cheers

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much

Collapse
 
adika_david_f4882f8904c91 profile image
ADIKA DAVID

Great write-up, Henry! 👏
The real-world analogies (especially the car system example) made the OOP concepts much clearer and relatable. I also like how you broke down encapsulation, abstraction, inheritance, and polymorphism in a beginner-friendly way. This will definitely help a lot of learners approach JavaScript OOP with more confidence. Looking forward to more tutorials from you. Cheers

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt • Edited

Thanks a lot

Collapse
 
bob_junior_06e527a9bec1ab profile image
Dr Mark

This is the best OOP in JavaScript article I’ve ever seen. This article is both enthralling as well as educative with key concepts well explained in a simplified manner. Thanks for sharing this great piece

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much

Collapse
 
ebiweni_lokoja_f8e97af1f5 profile image
EBIWENI LOKOJA

When it comes to programming nobody does it better.
Thank you for the eye opener.
I'm learning every day

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks for the nice comments

Collapse
 
justin_ikakah_4fe4f968621 profile image
Justin Ikakah

Great piece of work

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks

Collapse
 
alexander_ogbu_e55ffed921 profile image
Alexander Ogbu

I always look forward to reading great write-up from this fellow. Keep up the good work. Cheers

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much

Collapse
 
ebiasuodefelix_konyefa_4 profile image
Ebiasuode felix konyefa

Very impressive and carefully written…
👍👍

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks

Collapse
 
richard_ben_2ca6ff6b3c8ac profile image
Richard Ben

Very interesting write up, thanks for sharing Henry.

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks man

Collapse
 
oculus42 profile image
Samuel Rouse

Thanks for posting this! While I mostly recommend against OOP in JavaScript, I think it's an important paradigm to understand.

I think your Abstraction section isn't ideal. The way you define _balance and the related methods inside the constructor mixes actual OOP with closures. The getBalance and deposit methods are not members of the class this way, they are redefined for each instance.

This was how we managed private values prior to ES2022 when Private elements were added to the spec.

Using the private elements simplifies the design makes use of class-level methods.

// Abstraction example with a BankAccount class
class BankAccount {
  // Private variable to hold the balance
  // It cannot be accessed directly outside the class
  #balance

  constructor(owner, balance) {
    this.owner = owner; // account owner's name
    this.#balance = balance;
  }

  // Method to check current balance (controlled access)
  getBalance() {
    return this.#balance;
  }

  // Method to deposit money (changes balance internally)
  deposit(amount) {
    this.#balance += amount;
      console.log(`Deposited ${amount}. New balance: ${this.#balance}`);
  }
}

// Create a new bank account with an initial balance of 1000
const account = new BankAccount("Peace", 1000);

// Deposit money using the provided method
account.deposit(500); // Output: Deposited 500. New balance: 1500

// Access the balance safely through the method
console.log(account.getBalance()); // Output: 1500

// Direct access like account.#balance is not possible (hidden!)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sinni800 profile image
sinni800

Not the car analogy...

Also a lot of the "importances" are just really good weather sort of ideas. It does not guarantee reusability at all, for example, and with something like the "car analogy", it even guarantees that scalability will suffer. We should not model the code after the domain of the real world idea, but we should model it after the domain of concerns. Rather than making a car and "have the steering wheel take care of physics", make a physics class (or module). make subsystems that concern themselves with one technical concern, not with a domain concern.

Overall this article advertises the same tired way of doing things that has been making our applications slow and bad to maintain for a long time now. As a beginner of course someone would be impressed by it...

Uncle bob would be proud

what you should honestly do is watch this talk on the topic and maybe see what's going on youtu.be/wo84LFzx5nI

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks for your input.

Collapse
 
barak_codes profile image
Eli Barak

Nice Article