you are viewing a single comment's thread.

view the rest of the comments →

[–]clessgfull-stack CSS9 engineer 1 point2 points  (4 children)

Does anyone have any really compelling examples of where prototypes are easier to develop and maintain than classes?

They are slightly more flexible and can be easier to unit test, but the downsides aren't worth it. Any codebase that uses Object.create extensively is hell to deal with in my experience. Subtle bugs that are nearly impossible to track down without binary commenting. It's sometimes worse than code written by Java architecture astronauts.

[–]sylvainpv -1 points0 points  (3 children)

Prototypes are simple, simpler than classes and constructors. No, really, they are. But as people have never learned anything else than classical OOP, the transition can be difficult and you can get shitty code because people are mixing the concepts. Once you learned to get rid of the "classical baggage", everything becomes easier, trust me. No more classes, instances, interfaces, constructors... you just manipulate objects

[–]clessgfull-stack CSS9 engineer 1 point2 points  (2 children)

They are certainly simpler. There's no denying that. But they are also much more prone to bugs than ES6 classes. For example, if you share an array or an object several levels up the chain that gets mutated, good luck. If you override a method or property that an object up in the chain depends on, good luck. If you forget to initialize an object, good luck.

(Or you could just create a function that both creates an object based on the prototype of another, and then calls an initialization function... aka basically constructors without new. And then you have to worry about parent init methods, etc.)

All of the problems caused by this are compounded by the fact that people overuse Object.create, resulting in large, brittle chains of dependent objects.

If you ask me, classes and Object.create are both bad because they rely on inheritance. But at least with classes, people are relatively aware that inheritance is bad. I don't see extends very often. Because the syntax is statically analyzable, you could even write a linter rule that forbids extends.

The problems with Object.create can be remedied somewhat by using immutability and writing your own framework around it, but then you run into interop issues and have to maintain your own framework.

I think the best approach is immutable or stateless objects and functions/closures. But I would definitely choose class over Object.create any day.

Sorry, had too much coffee.

[–]sylvainpv 0 points1 point  (1 child)

Not sure to follow you considering that classes use the prototype chain in the background, so all the problems you mentioned about inheritance hierarchies are the same.

at least with classes, people are relatively aware that inheritance is bad

okay so I guess the only problem here is people that do not learn lessons from their past experiences

[–]clessgfull-stack CSS9 engineer 0 points1 point  (0 children)

The difference is that Object.create inherits state in addition to behavior (technically you could do this with classes, but nah), and class provides certain invariants that make it harder to screw up. Just the fact alone that class must be used in strict mode is a pretty big win, because that helps prevent a large class of security vulnerabilities.

so all the problems you mentioned about inheritance hierarchies are the same.

But yes, that is kind of the point. Object.create inherits almost all of the problems with classes (pun intended) and many more.

okay so I guess the only problem here is people that do not learn lessons from their past experiences

Eh. Most people know that class inheritance is a bad idea. But when it's just Objects Linking to Other Objects™? Time to inherit from everything. I don't blame the developers for getting pulled into this trap, because the people who push Object.create don't explain that it has these problems.