DEV Community

Cover image for Object Oriented Programming With JavaScript - Part 1 🚀

Object Oriented Programming With JavaScript - Part 1 🚀

Ali Samir on February 18, 2024

JavaScript fundamentally adopts Object-Oriented Programming (OOP) principles. OOP, a programming paradigm, revolves around representing real-world...
Collapse
 
eduardopazz profile image
Info Comment hidden by post author - thread only accessible via permalink
Edu Paz

I think it's important to emphasize that Javascript is NOT "fundamentally" OOP.

All the class syntax brought in ES6 is just a syntactic sugar for what can be done with plain functions, closures, prototypes and stuff.

It's way different from Java's OOP, for instance.

I personally prefer to work with plain functions and modules instead of replicating classes in a language not made for such paradigm

Collapse
 
efpage profile image
Eckehard

Though I do not like the expression "syntactic sugar" we still have to state that the class concept of JS has some serial issues:

  • JS lacks any kind of access specifiers. Many oop-languages do not only know private or public properties, but also ways to define, how elements could be accessed in derived classes
  • JS objects are not fully encapsulated, so you lack a most important concept of OO. You will still need JS workarounds like IIFE´s to protect your code.
  • the use of "this" in classes is a pure mess. It was a compromise to make class objects compatible to conventional JS objects, but leads to horrible code. In most languages, class code is not much different from code in functions and procedures, but in JS it is. You cannot define a variable in a class using let or var, but have to use "this" without any specifier.
  • Additionally, the new class based syntax and the old prototype based syntax can be used side by side which might make the confusion complete.

So, today you can use OOP methods in JS, but you cannot expect the same benefits like you get in other languages.

Collapse
 
urielsouza29 profile image
Uriel dos Santos Souza

Oop not class!
here is no one “canonical” definition of OOP. There are at least two of them.

Two big schools of Object-Oriented Programming | stereobooster.github.io

TL;DR There is no one “canonical” definition of OOP. There are at least two of them. From my POV there are two big definitions, which deserves separate names. I named them after biggest advocates. Those two definition have some differences and some commonalities. I was about to get into another argue on OOP on the internet. I decided to do some research on the subject. I found more than one definition of OOP. Some of the definitions are variations of the same “tune”, some are useless. On my opinion there are two main directions of thoughts in this domain, which from two schools of OOP. School as in school of thought. The same way as there were philosophical schools in ancient Greece. The same way there are two schools of OOP: Alan Kay’s school or message focused Bjarne Stroustrup’s school or class focused or “PolymorphismEncapsulationInheritance” in terms of c2 I must emphasize that this article is not about what is best way to do OOP, because such discussion will lead to holy war. Instead purpose of this article is to help to recognize the fact that there are two ways to think about OOP. I believe this will help to build more constructive discussions about OOP. Next time you will start discussion on OOP or OOP vs make sure you know which school of OOP you are talking about. About names I choose Alan Kay and Bjarne Stroustrup as founders of schools, because they are big advocates of OOP and authors of first OOP languages of each school correspondingly. I’m aware that there are arguments on which programming language is the first OOP language and who invented OOP. See history section. Kay's school Stroustrup's school

stereobooster.github.io
Thread Thread
 
efpage profile image
Eckehard

If you look into history, you will find far more definitions, like "Objects exchanging messages". The class based approach in JS is clearly inspired by languages like C++, where OOP is more an extension to organize your procedural code.

Collapse
 
alisamirali profile image
Ali Samir

Object Oriented Programming With JavaScript - Part 2 🚀

dev.to/alisamirali/object-oriented...

Some comments have been hidden by the post's author - find out more