Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The in -> out example for Classes (transforms.classes) helps me understand how ES6 classes work, as I was really confused

I'm still unsure what they offer over a simple object with property key values.



> I'm still unsure what they offer over a simple object with property key values.

They offer a particular abstraction, that gives a certain automatic conceptual clarity for free. Personally, I'm a massive fan of prototype-based object systems, but having the class syntax sugar on top of it has made certain patterns clearer to express even if it's semantically equivalent.

My main issue with ES2015 classes, is that I've seen some projects use it for everything including things that it's really not required for; really putting the _Java_ back in Javascript, so to speak. But that can be said for a lot of language features, really.


Most of the time simple objects/collections work... separating data from utility is often cleaner and easier to reason about.

That said, when you need encapsulation and instance isolation classes provide a nice sugar around prototyping. They tend to work very well for components that have visual interactions/events that are tethered to instance/scope state. I tend to prefer the React+Redux model myself, but there are times when you need more than render-only components, and in those cases the class syntax means cleaner source code imho. These cases are also very limited imho, and about the only place I use class syntax.


There are several technical differences between classes (or functions) and simple objects. Namely - presence of "constructor", ability to have static and non-static methods and overall the fact that class can be instantiated. It is easy to reason about. I'm sure there could be listed some additional features, but those are first things that pop into my mind.

However, I'd say one of the primary reasons for existence of classes would be familiarity. A lot of developers would have experience with classes in other languages so it is useful to have similar entity in JS.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: