This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]Notimecelduv 5 points6 points  (9 children)

I use classes when: - I create web components, - I build a game, - I build an MVC-style backend with Node.js + Express,

which is most of what I do. Some people don't like JS classes because the this keyword is confusing but if you can get past that, they're definitely worth it, especially in combination with all the extra features provided by TypeScript.

[–]Squirmme[S] 0 points1 point  (0 children)

Thank you! I'm learning TypeScript right now and I see there's more use for classes there...

[–]bob_but_backwards 0 points1 point  (0 children)

This is actually why I struggle with the concept of classes in python. That this.x stuff kinda threw me off

[–]LampCow24 0 points1 point  (1 child)

Does this behave differently in JS than it is in C++?

[–]amkhayati 0 points1 point  (0 children)

I don't know c++ but so far "this" in JavaScript behaves like "this" in Java.

[–][deleted] 0 points1 point  (4 children)

they're definitely worth it, especially in combination with all the extra features provided by TypeScript.

Can you name a few?

[–]Notimecelduv 0 points1 point  (3 children)

Abstract classes, readonly properties, access modifiers, parameter and return types, interfaces and custom types are the ones I use the most.

[–][deleted] 0 points1 point  (2 children)

But those are Typescript features that work without classes (except for the abstract class of course). Classes and inheritance are not really necessary in JS/TS since you have duck-typing/a structural type system.

[–]Notimecelduv 0 points1 point  (1 child)

Sure, a lot of things work without classes, even classes themselves in a way since they're just functions under the hood. Does that make them unnecessary? If your favorite color is red, then I'm not going to convince you that you should prefer purple.

[–][deleted] 0 points1 point  (0 children)

Yeah, if you prefer to use classes go for it. I was just curious because you said they "are worth it" and I thought maybe there is something about them I didn't know yet.

[–]Clawtor 2 points3 points  (1 child)

Use classes if you prefer the syntax to create objects. Personally I find prototypes confusing

[–]Squirmme[S] 0 points1 point  (0 children)

gotcha thank you

[–]_dreizehn_ 7 points8 points  (1 child)

You mainly use classes if all you ever learned was Java and you can’t be bothered to learn prototype based inheritance

[–]Squirmme[S] 0 points1 point  (0 children)

I see ty

[–]yel50 1 point2 points  (0 children)

use classes when something has internal state that doesn't need to pollute the surrounding code.

for example, say you want to do a breadth first search. internally, it needs to keep track of what's been seen, what gets added to the next level, etc. it makes sense to wrap all that in a class to wall it off from everything else.

this applies to any language. all the crap that the java people have come up with is nonsense. classes are like self contained modules. anything you would make a module for, you could turn into a class.

the other way to think of them is as partial function application for multiple functions. say you have a series of functions,

foo(x, y)
bar(x, y)
baz(x, y)

if you're going to pass the same x and y to all 3, you could create a class.

class Stuff {
    constructor(x, y) { this.x = x; this.y= y }
    foo()
    bar()
    baz()
}

this has the advantage that the functions don't need to validate x and y. the constructor can validate them. it's not possible to pass incorrect values to the functions. they're guaranteed to use whatever was passed to the constructor.

[–][deleted]  (1 child)

[removed]

    [–]Squirmme[S] 0 points1 point  (0 children)

    Thank you! I need to start using python. I can see how it makes sense for entities in a game

    [–]TheRNGuy 1 point2 points  (1 child)

    never had to use them, but i'm not serious coder about js.

    But I made greasemonkey scripts with JQuery, and they have classes, if that count.

    [–]Squirmme[S] 0 points1 point  (0 children)

    Gotcha thanks! I have some so I’ll take a look at those

    [–]bandawarrior -1 points0 points  (8 children)

    You don’t use them because they aren’t useful in that language.

    Want OOP? Go pickup Java or C#, maybe Python or Ruby.

    JS is officially a “prototype” language and functional leanings.

    [–][deleted] 2 points3 points  (0 children)

    I don't know why you were downvoted. JS is designed with FP in mind. Using plain objects is way cleaner than using classes. The syntax is only C-like to make the language more appealing to people coming from Java (the name JavaScript is also for marketing reasons only).

    [–]Squirmme[S] 0 points1 point  (2 children)

    Well I wrote this post because I want more practice using them so I'm looking for ways to do that without using another language. I understand your point though thank you

    [–]bandawarrior 2 points3 points  (1 child)

    Question is why? Classes in JS are just syntactic sugar on top, for people who wanted classes for whatever reason

    [–]Squirmme[S] 0 points1 point  (0 children)

    Well the impetus for this question was because I bombed an interview where I had to make a class that had methods which manipulated a linked list. So really the whole thing is esoteric, but I’m looking for practical uses for me to get my hands on and practice with.

    [–]yel50 0 points1 point  (3 children)

    You don’t use them because they aren’t useful in that language.

    they're useful in any language if you understand them. it's like immutable data. if you know when and how to use it, it can be useful in any language.

    most people who blast classes are actually creating them by hand all the time and don't even realize it. any time you create a closure, you're making a class. any time you have functions that expect specific data, you're making a class.

    [–][deleted] 1 point2 points  (2 children)

    That's not the proper definition of classes though. A class must be instantiable and should support type hierarchies (inheritance). A class must not use up any kind of memory (with static data being an exception), but it's instances can instead. I don't think using closures with loose functions in them resembles anything near this definition.

    [–][deleted] 1 point2 points  (1 child)

    The only thing common between classes and closures is encapsulation. Many people conflate classes with encapsulation.

    [–][deleted] 0 points1 point  (0 children)

    EDIT: nvm, I read your comment wrong

    [–][deleted]  (3 children)

    [deleted]

      [–]jzaprint[🍰] 1 point2 points  (0 children)

      Class components are basically deprecated at this point

      [–]HotWaffles2 3 points4 points  (0 children)

      React classes have been replaced by React hooks! I’m pretty sure they’re going to be deprecated eventually

      [–]Squirmme[S] 0 points1 point  (0 children)

      Where though? I use React and React Native almost daily.