DEV Community

Discussion on: You don't need classes

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️

classes are a good way to encapsulate a set of functions within your heal memory. Under the hood when you use class its like using .prototype however its shorthand and a bit cleaner.

By using classes within an object oriented language, you are able to maintain the code easier and better. It does the mostly by making the code much easier to read. Everything in JS is a function, including objects, symbols, maps, and classes for example.

Back in the days of <ES4 language the style of coding was very similar to your .prototype example code. for a simple one file module i would probably not use classes, I would use all functions and prototype to wrap the stuff up. Another key point to note here is that .prototype stuff works more like struct's and processes fast for things like three js or games that need to run at a very high frequency.

Collapse
lukeshiru profile image
LUKESHIRU Author

The example using prototype is just there to illustrate that the classes are still just functions in JavaScript. My argument in the post is against classes in JS, mainly because if internally you're still using functions, you can just use them directly enabling better reuse, composition, testing and so on.
You can use modules to encapsulate, if you really want to have an object with methods you can create one putting several functions together, but you can't go the other way around (reuse or compose classes). You'll end up with the classic inheritance problem of wanting a banana and getting a gorilla holding that banana and the entire jungle.

Some comments have been hidden by the post's author - find out more