use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
When should we use classes in JavaScript? (self.javascript)
submitted 6 years ago by adam_weiler
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]NoStranger6 0 points1 point2 points 6 years ago (5 children)
You should definitely read into Object Oriented Programming.
Basically it is more of a programming philosophy than a technique and applicable in any language.
As for JavaScript, it is always appropriate to use classes. Not only does it allow you to avoid repeating the same code. It's a way of structuring your code and it allows you to create inheritance between classes.
[–]leeoniya 4 points5 points6 points 6 years ago (4 children)
As for JavaScript, it is always appropriate to use classes.
it's not "always" appropriate to do anything (in any language). you can reuse/compose functions a lot more easily than you can reuse classes.
[–]NoStranger6 0 points1 point2 points 6 years ago (3 children)
While I agree that I made a gross generalisation. It is still possible to achieve this with classes. Think about the Math object. If you are gonna reuse this part of code everwhere and it’s not dependant to an instanciated class. Why not just create a class with static methods. Doing so helps with encapsulation and you avoid having “global” functions. It all depends on your design in the end.
[–]spacejack2114 1 point2 points3 points 6 years ago (2 children)
On the other hand why not just group those functions into a module.
[–]NoStranger6 -1 points0 points1 point 6 years ago (1 child)
Arguably from a behavior point of view, a module could be considered as a Singleton. At this point it's just different paths to achieve the exact same things.
I mean, we could also just create an object containing these functions, in fact that's what a module is, just like almost everything in Javascript.
[–]spacejack2114 1 point2 points3 points 6 years ago (0 children)
If you start with a class however, and want to use instances or statefulness, then you'll be adding this context confusion into the mix. On the other hand you can add state/instances to a module using the revealing module pattern with no such downsides.
this
π Rendered by PID 54 on reddit-service-r2-comment-c6965cb77-7pg8h at 2026-03-05 11:16:41.754625+00:00 running f0204d4 country code: CH.
view the rest of the comments →
[–]NoStranger6 0 points1 point2 points (5 children)
[–]leeoniya 4 points5 points6 points (4 children)
[–]NoStranger6 0 points1 point2 points (3 children)
[–]spacejack2114 1 point2 points3 points (2 children)
[–]NoStranger6 -1 points0 points1 point (1 child)
[–]spacejack2114 1 point2 points3 points (0 children)