4 ms·
I think we may be talking a little bit in two different directions, because when you write this: > Classes and types and methods exist for a reason. What that
by devjab 3y ago
I think we may be talking a little bit in two different directions, because when you write this:
> Classes and types and methods exist for a reason.
What that reads like to me is actually the exact explanation as to why the JS community has fallen out of love with things like classes. Because JS doesn't have static types, and without those, well... Classes don't actually exist. At least not in the way they do in other languages like C++. You can pretend you're working in a regular OOP language, and use the prototype object like you would a C# or Java class but since you don't have static dispatch at compile time, you're frankly playing this part of JS as a weakness instead of a strength.
Which is why the JS community has been "growing beyond" classes.
If working with classes in JS is more comfortable for you, then I think you should do so, but ultimately a JS class is just a function. Once you grow comfortable with working with modules and functions, you're very rarely going to be writing classes when working with JavaScript. You're still going to do so from time to time, when it makes sense, but often its both unnecessary, less efficient and harder to maintain something that is build like a C++ class rather than than a JavaScript function. While working with JavaScript, being a key part of it. This is not an attack on OOP in general, just an explanation as to why the JS community has "moved beyond" pretending JS is an OOP language the same way Java is an OOP language.
> Try stepping outside of JavaScript for a while, broaden your experience.
I do so regularly.
- andrewmcwatters 3y agoA JavaScript class is syntactic sugar for prototypes, not functions. JavaScript is a prototype-oriented language.
- devjab 3y ago> A JavaScript class is syntactic sugar for prototypes, not functions. Again you're basically speaking into exactly what I'm trying to say. Because what are JavaScript functions? It is also a prototype. I'll try to put it a different way. The JS community has "moved beyond" classes because they are functions. Both classes and functions are prototypes, but functions are often easier to work with, more maintainable and more efficient. I'm not trying to say that you should never write another JavaScript class in your life. I write them myself when it's prudent to build things in a structure that resembles classical OOP. The reason I don't personally use that sort of coding style, if I can avoid it, is because it's an act. It looks like classical OOP, but everything is mutable at any time which means it's very easy for consumers of the code to break it by accident. This is less of a problem when you build things as isolated modules, based on individual functions, because there simply isn't any complexity to "misuse". This is sort of similar to why the virtual DOM became so popular in frontend JavaScript. Because it's harder to work directly with the DOM itself. It's not necessarily better to use functions, it's just "safer", because they come with less risk of someone doing something bad by accident down the line.
- andrewmcwatters 3y ago…no a function is a function and a prototype is a prototype.
- fendy3002 3y agoUhh yes, classes in JS is just function, but it forces you to make a strict definition on how the set of functions will behave, in opposite of defining functions and tied them up individually. If a thing need properties and methods to operate, defining it as class won't make it harder to maintain than cleverly do it with functions and object.
- devjab 3y ago> but it forces you to make a strict definition on how the set of functions will behave This would be true if JavaScript had static dispatch, but since it doesn't, everything you do with your class is mutable all the way up the prototype object chain. One of the reasons this is harder to maintain, is because the "strictness" you think you build into your code isn't actually there unless everyone who works on your code after you follows the same rules you did. I'm sure we'd like to live in that world, but I doubt we will.
- tracker1 3y agoIt's slightly more now with private members, but still prototype based inheritance and definitely not the same. Composition will still usually perform better all said. If you want something more, better to use TypeScript or step to something like Rust+Wasm.