HACKER Q&A
📣 4ipp

Why is there a tendency in JavaScript to omit using classes?


I see that it is often suggested to use functions instead of classes. For instance, the latest React updates suggest using hooks so that you don't need to write a class. I've seen similar tendencies in other JavaScript libraries.

I write JavaScript only occasionally and I am not a JavaScript expert by any means, but I find classes useful. Can you tell me where this tendency comes from? Why there are large code bases without a single usage of the "class" keyword?


  👤 jfengel Accepted Answer ✓
Because Javascript doesn't do much type checking, you lose some of the biggest wins of class-based programming. And as a late addition, it was a bunch of new stuff to learn that didn't appear in legacy libraries. It would have needed a significant win to really boost that. And it just didn't have it.

JS was intended as a scripting language. You didn't spend time crafting your types so that you could do large-scale projects (multiple developers and/or long time spans) that typing really helped. JS's classes didn't do enough to enable that, so JS was never really used for the types of business logic that is most effective for types.

Typescript does add that. But TS pushes towards a function-based development style. Classes add ways of organizing code to that, which is good, and TS makes it more useful. But TS still isn't much adopted to the large-scale projects that really make best use of it. You can use TS non-class Types and do almost as well without having to learn all the new syntax (and more importantly, semantics).

OO is oriented a lot around encapsulating mutable state, but development style really pushes these days around immutable state. It's just easier to debug and scale. (At a performance cost, but if performance is what you're after, you're not writing Javascript.) So function-based code is what you get.


👤 ramitmittal
I believe it's because ES6 "class" syntax is a recently added feature (in programming language years). I use classes in JavaScript a lot, but I have to admit that it's mostly for the cleaner syntax and less for the OOP features. However I have to admit that I've also noticed the trend and would really like to know if there's another reason behind it.

👤 joshxyz
When you instantiate a function(){}, you can get private class properties. Cant get that on es6 classes without babeljs. Cant instantiate es6 arrow functions too.

On reactjs, the flow is much more smoother with hooks instead of class instances because there is no more need to bind functions to your class instances in your class constructor;

there is no need to refer to `this` (on hooks you work with vars directly), and on hooks you are forced to treat things immutably (when you are calling set_value(new_value) instead of mutating the class state directly;

and the useEffect hooks give you a better control and grip on the react component lifecycle.

I honestly avoid usage of es6 classes unless I have to extend another class (e.g. extending Error for custom errors, or extending nodejs EventEmitter).


👤 sethc2
I find classes in JavaScript just more tedious, and they’re really just syntactical sugar on top of a function anyways. Working in react I’m always making things immutable, and I generally use typescript, and classes to me are just more painful to write then a function whose parameters act effectively as constructor arguments and the return statement which is the classes public API.

👤 jeffreygoesto
Maybe it's like in C++, where free functions were advertised for a number of usecases? Discussion with links to more background [0]

[0] https://www.reddit.com/r/cpp/comments/262it3/effective_c_pre...