re: Do we really need classes in JavaScript after all? VIEW POST

FULL DISCUSSION
 

Even though javascript is a 'functional' language, I would bet that most javascript developers write OO code. If you remember the javascript prototype chaining we were doing a few years ago which mocked the class behavior, the class syntax was a natural next evolution in syntax. I don't think it hurt anything.

After looking at your example, I was a little confused about the decoupled increaseCount function. It looks like it should be a method that belongs to SimpleCounter. That was my natural reaction. But now after thinking about it, I think we could use a pure functional component library.

I am not sure what it would look like completely, but this is a great start. I would look into seeing if you make state it's own entity like react hooks did. But use curried functions like you would find in Ramda to make the mutations. Each state entity would basically have its value, but it returned a function that handled the next mutation.

 

Of course, I remember definitions on prototypes. But then, I didn't understand very well what's was going on with them. Classes made this simpler and cleaner. Also, they introduced own problems.

The side effect of the SimpleCounter is outside of the definition in purpose. It is private, so users can't trigger it accidentally. It might be strange at first, but it has own sense.

This concept is also great for testing. As you can see, we can export side effects, and create unit tests for them without component initialization. They are just simple functions.

I am not sure what it would look like completely, but this is a great start.

This is exactly what I want, a discussion about better web development. And I will be my success if I encourage someone to create next great ideas.

code of conduct - report abuse