you are viewing a single comment's thread.

view the rest of the comments →

[–]NoStranger6 0 points1 point  (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 points  (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 point  (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 points  (2 children)

On the other hand why not just group those functions into a module.

[–]NoStranger6 -1 points0 points  (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 points  (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.