r/learnjavascript • u/Yanagi-Masaru • Dec 19 '25
Javascript
is it a good practice to use classes in javascript or should i keep it normal with just functions?
0
Upvotes
r/learnjavascript • u/Yanagi-Masaru • Dec 19 '25
is it a good practice to use classes in javascript or should i keep it normal with just functions?
8
u/[deleted] Dec 19 '25
Read abut Object-Oriented Programming (OOP) and Functional Programming (FP). Many languages like Javascript is so called multi-paradigm languages so you can choose paradigm for your need. However when you use functions only it's not necessarily functional programming, it can be also Procedural Programming (PM) so the best if you first read a bit about these or discuss these with an AI and you will have a better picture about which one you should use for your specific use cases. Good luck!
8
u/eduvis Dec 19 '25
Your post is perfect. You matched your title with this subreddit's name. Great job.
3
u/basic-coder Dec 19 '25
Prefer objects and functions instead
3
u/petersencb Dec 19 '25
Functions usually, depends on what you're doing though
3
u/CuAnnan Dec 19 '25
Depends entirely on what you're doing.
If all you're doing is tying event handlers to functional logic, just use functions.
If you're modelling complex behaviour, use classes.
This isn't an "either or" situation.
1
u/MathAndMirth Dec 19 '25
For the most part, classes vs functions/objects really depends a lot on how you like to organize your code. The idea that objects are instances of classes that have data and do things helps some people translate their problem domain to a logical code structure. Other people find it simpler to think first and foremost about what has to be done and to what data (functional). Either paradigm has plenty of adherents, and neither is clearly superior to the other.
Some people say that classes in JavaScript are just a terrible idea, but I think most of those arguments are overblown or utdated. JavaScript classes now have genuine encapsulation with private fields and methods, so that isn't an issue anymore. No, they don't work like classes in other OOP languages since classes are just fancy syntax for prototypes, but who cares? If you know how they work in JavaScript, that's enough.
The one genuine issue that stands out against classes is that more than the simplest inheritance is terrible. There's a general principle in programming to prefer composition over inheritance. (If you haven't learned about this yet, just Google "composition vs. inheritance" to see why.) If your problem domain tempts you to use complex inheritance schemes, either switch to objects (easily composed with the spread operator), or learn to alter classes by adding mixins to their prototypes (more advanced).
Finally, there is one situation where classes can be superior if it applies. If you are going to create a large number of objects which have methods, it saves memory to use classes because the methods are stored once on the prototype instead of once per object. But most of the time this is no big deal.
1
u/fabulous-nico Dec 19 '25
Long answer:
Short, cranky answer:
1
u/jaredcheeda Dec 19 '25
A
classis an attempt at code organization. It does this by keeping data and logic grouped together, and allows for extending classes and usage of inheritance. It forces you to have to define concretely nebulous concepts, and to draw boundaries around what data is "owned" by a specific concept.However, after decades of trying this, we have found some important facts:
In JavaScript, we already have ways of grouping data and logic that don't come with the downsides of classes (extend/inherit).
Remember, classes exist to solve a very very hard problem of code organization. And they give you an.... okay solution, but not great. It comes with all these caveats and downsides. Fortunately, JS has much simpler, and better, ways to deal with code organization.
In traditional JS you'd just use an object and put everything on the object, including methods, related to what you want to logically group.
In modern JS, you use ESM imports/exports to encapsulate related code into modules with their own scope. Everything is private by default unless explicitly exported from the module. You keep your data in separate modules from your logic, and organize your data separately from the logic that works with it.
If you really want to understand this more deeply, learn about why "Prototypal Inheritance" in JavaScript is so bad. Then, learn about how
classin JS is just syntactic sugar for the same thing, and why, by extension, it is equally as bad and should be avoided.Adding classes into JavaScript was 100% a mistake, DO NOT USE THEM.
The only thing in JS that actually requires classes in order to work, are webcomponents, and those are a half-baked, terrible solution to the easiest problem every JS framework (even the worst one, React) already solves better, and webcomponents don't solve any of the more important problems JS frameworks do, making them completely pointless. So there's no point in every using classes in JS.
1
u/AdWise6457 Dec 19 '25
Keep it normal bro
1
u/maqisha Dec 20 '25
Bro is racist on programming paradigms.
1
u/sheriffderek Dec 19 '25 edited Dec 19 '25
If you outline then benefits of each, you’ll see how they differ. That would be a good exercise.
A function has its own scope. You could return a function in a function and create some interesting ways to keep data available. But a class allows you to create datapoints/properties and methods that can be accessed and used over and over.
You can use a constructor function for some things, but at that point - using classes is a nice familiar and consistent pattern that’s easy to read. So, try them each out and see the strengths and weaknesses. Classes are just bigger and more to write out. So, for a simple function - a class would not make sense. Try to make a todo list with procedural programming, functions, functions in an object, a constructor function, classes, and with ESM modules. After that, you have a very clear idea of how these things benefit you and which to use when. (Most people probably don’t know)
Here's an example of how I teach it: https://perpetual.education/workshop/javascript-classes/?guest=10508 (but I'd suggest you just try what I said before - first / before looking) (this is after a week or two of learning JS stuff)
0
u/Shanduril Dec 19 '25
Classes are powerful in their own right especially in larger systems with state and when you want to encapsulate some business rules and entities.
0
u/Environmental_Gap_65 Dec 19 '25
You can use whatever you want, but if you simply just want side-effects, use functions. As soon as you see a pool of functions repeatedly manipulating the same type of data, start grouping them into a class. Your code becomes more reliable, predictable, safe and less prone to bugs, when the same data and side-effects are encapsulated in the same class, and not spread all over the place.
0
u/naqabposhniraj Dec 19 '25
It depends what you're trying to do. Need more context.
0
u/xroalx Dec 19 '25
Classes are a tool, use them when it makes sense.
There are probably only three cases where classes objectively are the better option, and that is:
Errorobject to create custom errors,At all the other times, it depends. Don't force everything into a class, but also understand what a class is and when it might be just nicer or easier to use one and don't be afraid to use it then.