r/learnjavascript • u/pptzz • Mar 19 '26
What's the use of classes in JS
I've recently started learning JS and I can't see a use for classes. I get how they work and how to use them but I can't see an actual real use for them.
40
Upvotes
View all comments
Show parent comment
1
u/azhder Mar 19 '26
No, that’s nonsense.
Closures are just a piece of memory created in a specific manner (lexical scoping).
Classes are mathematical term that means a subset created by partitioning another set using an equivalence relation.
Let’s imagine a function that returns true/false. The function has encoded the relation “has the same number of legs”. Now get a set that contains: chair, table, human, chicken, horse, whale…
If you draw that big set and draw lines connecting the items in it, like human connecting chicken, horse connecting table and chair, you will see they are grouped in their own separate subsets that don’t relate to each other - classes.
So, a human or a chicken will be an instance, a representative, of the class two-legged.
The class syntax is just an elaborate way to encode the above, but you can simply use a closure for encapsulation, put the equals function in the mix and voila, you got classes without a class syntax.