What are the main differences between prototype-oriented programming and class-oriented programming?

Asked

Viewed 191 times

7

After learning a little more about Javascript, I realized that even though I have a building class, classes (in fact, as in C# or Java) do not exist in Javascript. This is nothing more than syntactic sugar for language prototyping-oriented programming.

I already know how prototypical programming works, but I don’t really know the differences between it and class-oriented programming (true, as in Java or C#).

So the question is:

  • What are the main differences between the orientation to objects via prototypes and orientation to objects via classes?
  • What are the advantages and disadvantages of each of these standards?

2 answers

4


Well, the question doesn’t focus on the specific implementation, as it is that of Javascript, so I won’t talk so much about the points that JS chose so, even though it is the most popular implementation of this form of object orientation.

I’ll start by asking you to read What is the difference between a class and an object?.

If the class does not exist, the model is the object itself. And this can be considered advantage or disadvantage since you don’t need to write the model, write directly an object, which is simpler, but it changes a little the mental model, which hurts one of the main points that object orientation was created that is thinking about what that object is.

A clear disadvantage is that any meta information needs to be within the object itself, in some cases loaded on all objects or on specific objects. This is clearly less efficient. Flexibility charges a price.

It is very flexible to modify the prototype in the way that is best for home case, but at the same time it makes the contracts more fragile. Everything is easier on him, including making mistakes.

The languages that opted for the class, in general, are of static typing and value more for the contracts, after all the class (real) is a rigid contract. Nothing prevents these languages from having a prototype or false class shape more or less convenient, and thus giving more flexibility as well, in the same way that they can give optional dynamism to typing. When you choose to have no class there are elements missing and these languages can never have the same characteristics as the ones that opted for the class.

The prototype makes it very difficult to maintain static typing, each change in the object has the potential to change its type.

Class is about being more explicit. With the prototype everything can happen, you have to be more prepared, which is curious because the ease it gives charges a price that makes it more difficult, and if you do not know what you are doing you will have to rely on luck, the compiler can do little for you.

The fake class can already help a lot, which is what Javascript, and especially Typescript, did. It gives information that helps the compiler make decisions and many of the above problems are solved. But it doesn’t make the object more efficient, it still has the weight of a prototype, it has the infrastructure to give unused flexibility (at that point). It’s the same as putting static types in variables in languages like JS, Python, PHP, Ruby or other, helps give robustness, but the price of the dynamic object remains there.

So we can say that this has a little bit to do with the old problem about what is object orientation and has links with the two schools of this secondary paradigm (see more). The prototype appeals more to Alan Kay fans and the class appeals more to Bjarne Stroustrup fans.

For me the differences, advantages and disadvantages are almost the same as the dynamic and static typing. She doesn’t charge you to come in, she charges you for everything you do afterwards.

MDN table:

Category Class-based Based on a prototype
Class vs. instance Class and instance are distinct entities All objects can inherit from another object
Definition Define a class with a class definition; please install a class with constructor methods Define and create a set of objects with constructor functions
Creation of a new object Create a single object with the operator new Ditto
Construction of the object hierarchy Construct an object hierarchy using class definitions to define subclasses of existing classes Construct an object hierarchy by assigning an object as the prototype associated with a constructor function
Inheritance model Inherit properties following the class chain Inherit properties following the prototype chain
Extent of properties The class definition specifies all properties of all instances of a class. Unable to add properties dynamically at runtime The constructor or prototype function specifies an initial set of properties. You can add or remove properties dynamically for individual objects or for the entire set of objects

I think it’s worth reading Javascript is an Object-Oriented language?. And There is a class in Javascript?.

For further deepening almost always the best sources are in mother of wikis.

-1

Prototypical heritage is a fairly simple model of code reuse, code can be reused directly and helps us reduce redundancy.

Disadvantages are the lack of private and protected variables and unsafe types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.