I rummaged through a bunch of resources, but I didn’t find anything about advantages and disadvantages.
- 2In JS, there is no class-based inheritance. Only prototypes, only hardcore - Dmitriy Simushev
- I know this very well, I need the advantages and disadvantages of prototypes compared to the classical inheritance, for example, in Java, just rating does not allow me to leave more specific tags - Abraham
- These are just different things, each with its own advantages and its own problems. You do not ask "what are the numbers better than the lines?". - VladD
2 answers
In theory, prototypes give more flexibility. With their help, you can get up such that the strict PLO never dreamed of. Therefore...
In theory
Benefits:
- Magnificent flexibility: the ability to change prototypes during program execution, generate dynamic prototype hierarchies and perform other fun things.
- You will not be able to dismiss, because only you can support the code.
Disadvantages:
- Nobody will understand your code.
However, if you apply this theory in practice, then you will be laughed at for the abnormal programming, otherwise they will be asked to quit. Almost always, you will use only those capabilities of prototypes that neatly fall into the PLO. Moreover, the existing javascript engines assume that you will use strict "classes", and when you go into the dynamics, they slump in performance. So go to the harsh truth of life ...
On practice
Benefits:
- None.
Disadvantages:
- Strange syntax.
- The zoo of libraries that emulate regular classes.
And let's be honest: the only mainstream language with prototypes is JavaScript. This is a language that was designed 10 days and laid out for a couple of months. The new version of the standard introduces syntactic sugar for classes, so the old syntax with prototypes will be a thing of the past. Therefore ...
Soon
Benefits:
- If you know what prototypes are, then you will be 20% cooler than those who do not know.
- You can be proud of this absolutely useless knowledge.
Disadvantages:
- None.
- Are there more or less realistic examples where this “mind-blowing flexibility” might actually be needed? And then I also sit just emulating classes and I don’t know, maybe I don’t understand what - andreymal
- It seems to me that, having an ordinary OOP, it would be possible to simulate the protopic one in the same way. (Create a hidden member of
Proto, and resolve all calls to public members manually.) - VladD - @andreymal This flexibility cannot be needed: no one will understand this code, because changing the type hierarchy during program execution and other magic will break the brain with conceptual complexity. Someone probably does that, but I wouldn’t have approached this code as far as a gun shot. - Athari
- 2@VladD On Tutring-full language you can emulate anything. Classes, prototypes, method calls through dot, and so on - these are all just syntactic sugar. No one bothers to make virtual method tables, method scheduling through lines, and so on, even in turbo-pascal, or even in assembler. The only question is the number of letters. All "programming patterns" are patching holes in the limited syntax of programming languages. - Athari
- @Discord: Yeah, especially the last line strongly agree. - VladD
Since, based on the prototype inheritance, it is possible to implement the classical one, its only drawback is some complication and unusualness of the code.
- 3And on the basis of the classic, you can implement the prototype. Both will be a crutch :) - Pavel Mayorov
- @PavelMayorov, on the basis of prototype inheritance, you can make an honest classic. Based on the classic, you can only implement the prototype pattern, which is still not the same as prototype inheritance. - Qwertiy ♦
- oneDo not make a pattern. Prototype inheritance should be done :) - Pavel Mayorov
- @PavelMayorov, in this case, I propose a separate answer to demonstrate this. - Qwertiy ♦
- The most important thing about the merits I found, but about the shortcomings of anything, really ideal? - Abraham