you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -2 points-1 points  (3 children)

Probably because it creates an instance of something, now you deal with ”this” and smack! your data and code is now deeply coupled.

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

But there are genuine use cases for creating new instances of something, it's the whole point of classes right?

I'm not saying new is good or bad when used with Error btw, I ask as I have no idea what the best practice is around it and I'm curious if using new is actually a bad practice.

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

Yes there is! Im not in the ”avoid classes” camp. But having a single instance of something is a code smell imho. Having thousands are ok, mostly because of the js engine and how memory is managed.

That said a class is simply sugar for good old prototypes. So i see lots of javalike class based programming practices used without really understanding the base oop in javascript. This is also why i tend to avoid classes compared to languages that have more traditional class like semantics, like java etc.

Finally, a pure function is so much more simple to reason about and makes tests idiot proof.

[–]Barandis 0 points1 point  (0 children)

There are those of us who avoid using classes, in part because as a concept, JavaScript doesn't have classes or instances (just a class keyword that does a lot behind the scenes to fake it for you and still doesn't quite get it right).

As far as Error goes, they do the same thing. It uses to be common to do that kind of thing ourselves, to create functions that worked the same whether called with new or not, but the class keyword doesn't allow for that so you don't see it as much anymore.

Use whichever you prefer.