DEV Community

Discussion on: Is JS an OOP Language?

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

OOP is pattern, not a type of language, meaning you can write object-oriented programming in C, it will work, it will just be verbose and ugly.

Hello GObject !

It follows that you can also do OOP in JavaScript. Whether you should do OOP in JavaScript is an open-question. Functional Programming seems more natural to me in JavaScript, although one doesn't exclude the other.

Your explication of composition and composition over inheritance is very confused IMHO.

Neither composition nor inheritance has anything to do with passing parameters to the constructor. This is just the right thing to do in general because it's better to construct your object at once than to create an half-valid object and relying on mutability and on the programmer's discipline.

"Favor composition over inheritance" (.-.) it doesn't mean is that sub-classing is in any way something to avoid. When a sub-class truly is-a part of it's parent class it works just as well as composition.

This is vague. Saying that there are two approaches and neither is valid in 100% of the cases is not very helpful guidance. What "Favor Composition over Inheritance" does it to take stand, and affirm that you should prefer in general an HAS-A relationship. Valid IS-A relationships are the minority.

Wikipedia explains the benefits here en.wikipedia.org/wiki/Composition_...

To favor composition over inheritance is a design principle that gives the design higher flexibility. It is more natural to build business-domain classes out of various components than trying to find commonality between them and creating a family tree. For example, an accelerator pedal and a steering wheel share very few common traits, yet both are vital components in a car. What they can do and how they can be used to benefit the car is easily defined. Composition also provides a more stable business domain in the long term as it is less prone to the quirks of the family members. In other words, it is better to compose what an object can do (HAS-A) than extend what it is (IS-A).[1]
Initial design is simplified by identifying system object behaviors in separate interfaces instead of creating a hierarchical relationship to distribute behaviors among business-domain classes via inheritance. This approach more easily accommodates future requirements changes that would otherwise require a complete restructuring of business-domain classes in the inheritance model. Additionally, it avoids problems often associated with relatively minor changes to an inheritance-based model that includes several generations of classes.

For a good example of what goes wrong if you prefer Inheritance over Composition, have a look at my article on the Android SDK :P

Collapse
 
dendihandian profile image
Dendi Handian

The last time I used C language (6 years ago), it doesnt have OOP functionality but C++ is.

Collapse
 
jwp profile image
John Peters • Edited

Thanks for input JMF, the article has been changed to highlight your points.