|
|
|
Ask HN: Why is there a tendency in JavaScript to omit using classes?
|
|
4 points
by 4ipp
1923 days ago
|
|
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? |
|
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.