Hello, everyone 👋. I am a newcomer when it comes to JavaScript. I come from an OOP background (C# and Java). I’ve recently learned that ES6 has a class keyword that preforms similarly (but not exactly) to common OOP languages. Normally I would be inclined to use this feature in my projects; however, it came to my attention that the usage of class in JavaScript seems to be heavily discussed (mostly in a negative light). My questions to this community are:

  • Should it be used often, sparingly, or be outright avoided?
  • What are its advantages and disadvantages?
  • Are there specific cases where the usage of class excels?

Please share your thoughts.

  • atomic peach@pawb.social
    link
    fedilink
    English
    arrow-up
    8
    ·
    1 year ago

    The class keyword exists for a reason and it has a perfectly fine use case when you need to make use of creating new objects. I think it may be disliked because people come over from Java assuming you need to define everything as objects/classes when we have modules and other methods of doing the same thing with a little bit less clutter. I recommend reading up on the underlying functionality and how classes and objects work compared to modules.

    • heartlessevil@lemmy.one
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      It’s disliked because it uses a “class” keyword but it isn’t a class. JavaScript has prototypes, not classes.

      • object_Object@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        I somehow feel like there’s an allergy of sort towards classes in general in JavaScript/TypeScript. Many projects I’ve worked on gravitate towards more functional/plain-old-objects sort of paradigm and it feels like classes are avoided just because they don’t feel like idiomatic JS.

      • DonjonMaister@programming.devOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        Yes. This seems to be one of the common arguments against “classes”: they’re not exactly classes in the traditional sense, and everything you can do in JavaScript can be done so without touching the class keyword. It’s basically syntactic sugar that adds more confusion to the language, or so I heard. I’ve read this article that explains the flaws behind with this feature: https://www.toptal.com/javascript/es6-class-chaos-keeps-js-developer-up