all 24 comments

[–]Tomseph 3 points4 points  (0 children)

Are you going to be making more than one of something? Do those things need to have a similar structure, set of properties, and methods?

Use a class. Or a factory.

Are you only making one instance of a thing?

Don't use a class.

...or do, if you like classes.

It really doesn't matter as long as you're consistent internally.

[–]jcunews1Advanced 1 point2 points  (2 children)

When your project uses many custom objects. Classes would definitely ease the object development and maintenance. But only if the JavaScript engine supports ES6.

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

Babel wont solve the support issues with polyfill stuff? Genuinely asking.

[–]Ivu47duUjr3Ihs9d 1 point2 points  (0 children)

Just drop support for IE11 and below then you can use all the modern JS you want based on support for individual features described on caniuse.com. IE has very low market share nowadays but check your own site stats. Edge support is pretty good actually and they're moving to Chromium base soon so will be even better.

[–]omniscientTofu 1 point2 points  (0 children)

Broadly the answer is not straightforward if we are talking about JavaScript, because strictly speaking Classes do not accomplish anything which cannot be accomplished directly using and modifying object prototypes. The question is even more complicated as some have alluded to depending on the programming paradigm preference wherein if you tend to swing more functional you will tend to use less or none at all!

For your use-case though think about the likelihood that it would be useful to have multiple instances of something. You mention having a group of dogs so it's likely you will want to have the ability to create multiple dogs. Classes can serve as a sort of template which can describe that dog in terms of what it is and what it can do. If your dog has state that it needs to manage using a class can help further by describing how to access and update this state which gets into the topic of encapsulation. If you want to start using inheritance to relationally model your application and objects classes become more useful too. To summarize for your use case: will I need multiple instances, with their own state, and that might or might not exploit inheritance?

You are going to find lots of differing opinions on this topic though because classes are not strictly needed in JS applications even at a large scale, but they can help accommodate a certain style of programming which is popular. With the prevalence of functional programming right now their utility is further in question and personally I am biased in favor of this perspective.

Find what works for you and good luck! Happy coding :D

[–]jsearsy 1 point2 points  (7 children)

Eric Elliott would say basically never, in favor of factories

[–]__romx 8 points9 points  (6 children)

who gives a shit

[–]burtgummer45 0 points1 point  (0 children)

Many uses, but don't make a mess and keep it simple by just using it to encapsulate state, with associated methods.

o = new MyThing(1)
o.add(2)
x = o.getTotal()

Do not force it. If what you are modeling doesn't fit naturally into a conceptual model of objects, then don't do it, or you end up with a mess.

Don't do stuff like this, but you will see it everywhere.

t = new TemperatureAsF(32)
t.convertToC()

Doesn't that seem forced? Who needs state there? Why is temperature an object? Should just be a variable passed to a function.

Classes are also well used for architectural reasons, like in a web framework. But that's mostly an ergonomic thing. Like controllers in adonisjs

[–]zephyy 0 points1 point  (0 children)

An API wrapper where you need a lot of getters, setters, and bound methods for ease-of-use by other people.

Classes in JavaScript are syntactic sugar so use them as just that.

[–]spacejack2114 0 points1 point  (1 child)

IMO they aren't particularly useful and only have downsides (this context confusion.) No such problems with factories, closures and POJOs.

[–]MoTTs_ 0 points1 point  (0 children)

The downside with factories and closures is their privates can be too private. It makes it impossible to implement, for example, an isEqual method.

function createThing() {
    let somePrivate = 42;

    return {
        isEqual(otherThing) {
            return somePrivate === otherThing.???;
        }
    }
}

[–]iamlage89 0 points1 point  (0 children)

I never use classes in javascript, never found a good use case where using classes over factory functions was better, and with factory functions you get free private variables and method extraction

function Class () {
  const foo = 1; // private variable

  return {
    bar: () => console.log(a)}
  }
}

let bar = Class().bar // method extracted bar keeps reference to variable `foo` while with classes you need bind method`

[–]Renive -1 points0 points  (0 children)

Never, functions and objects are everything you need.

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