Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use class based OOP style inheritance in javascript?

If I'm not completely wrong every framework/library/approach in javascript tends today to mimick class based OOP style inheritance. Reasons for this seem to be people thinking class based OOP inheritance is far easier to understand and that most programmers know OOP.

In my experience I don't find evidence for either of this opinions. I think javascript prototypal inheritance is just fine (and I doubt the usefulness to force another paradigm upon a language than the one it is built on). Most of the developers I meet aren't even that good in classical OOP either. So what are the reasons to choose classical OOP style inheritance over prototypal inheritance?

like image 367
Norbert Hartl Avatar asked May 04 '09 20:05

Norbert Hartl


People also ask

What is the purpose of class inheritance?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class.

What is class-based inheritance in JavaScript?

Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class. Inheritance is a useful feature that allows code reusability.

Why we use object-oriented programming in JavaScript?

Object-Oriented Programming is a way of writing code that allows you to create different objects from a common object. The common object is usually called a blueprint while the created objects are called instances. Each instance has properties that are not shared with other instances.

What's the difference between class and prototypal inheritance?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.


2 Answers

I think the answer is in your question - most programmers are far more familiar with class-based OOP than prototype-based.

In fact I'd go so far as to say the majority don't believe you can have objects without classes.

like image 152
Greg Avatar answered Sep 21 '22 07:09

Greg


note that even if you're arguing for prototype-based OOP, you call it 'prototypal', and class-based OOP just 'OOP'. so, you yourself suffer from this bias, thinking OOP=>classes, prototypes => something else.

and since most people think that OOP is the right way no matter the problem, then prototypes must be inferior.

so, to answer your question, it's two factors:

  1. a bad definition: "OOP => classes"
    • propaganda: "it must be OOP, or you're not worthy"

since you're still bounded by the first, you try to explain that prototypes are an exception of the second. much easier is to correct them:

  1. there are many ways to do objects, classes are just the easiest one for static languages. most people are taught to program with static languages, and most of them try to use any language just like the first one they learned.

    • there are many ways to structure a programming solution, OOP are great at some and lousy at others.
like image 42
Javier Avatar answered Sep 19 '22 07:09

Javier