re: What was the most difficult thing to learn about JavaScript for you? VIEW POST

TOP OF THREAD FULL DISCUSSION
re: For me, it is classes and OOP. I know the syntax, I know how it works. but I don't know where and when to use it. I have been coding in Express.js...
 

You don't actually need to use classes in JavaScript. Some frameworks might encourage you towards them but still... it's an option. I understand the wish to learn how they work but functional JS is fine :)

I hope this suggestion helps with the fear!

 
 

I went through a couple of years loving classes until I realized that I didn't really use them to base or extend anything and I was in fact not doing OOP, classes are synonymous with Oop but really it's just a data structure. You could even do functional with Classes. Ultimately the humble Object literal is pretty much offering the same thing.

const ClassLike = {
   new() {
     return Object.create(this, {})
   },
   publicProp: 2
}

const inst = ClassLike.new();

So with that I could even do async construtors, multi inheritance and much more (for better or worse).
The one or two things I would miss, class is the only syntax where decorators are supported and I suppose the new class features like real privacy class field syntax do make classes 80% more appealing again.

I have been working on this thing and it has to be a class to write it this way.

class SimpleRouter {
   @get ['/home']
   ({ res }) {
      return res.html();
   }

   @post ['submit/myform']
   ({ somePlugin, res }) {
      somePlugin.handleData(res.data);
   }
}
code of conduct - report abuse