What was the most difficult thing to learn about JavaScript for you?

github logo ・1 min read

Hey all!

I'm curious as to what are or were some of the stumbling blocks you encountered when learning JavaScript? What are some of the gotchas that you wish you'd know about before hand.
Just a few things off the top of my head are:

  • Promises
  • Asynchronous handling
  • Closures

Please leave your comments below. I want to start covering some of these topics if I haven't already. Cheers!

twitter logo DISCUSS (21)
markdown guide
 

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 with functional programming for almost 2 years and not yet used Class based coding in backend. I don't know yet, if I can use there, or how to really use OOP concepts in Node.js. It really scares me !!

And another one is the prototypes in JavaScript.

 

Classes are definitely one of those things in JavaScript that were added for OO Devs (c#, Java, etc). If you don't need them and are fine with functional programming, then go for it.

 

I'd like to point out that JavaScript has always been an object oriented programming language, at least as far as I've known about it.

Prototypal inheritance is OOP. It's just that we tend to think that the only type of OOP is class based inheritance.

JS didn't change, it still uses protypal inheritance and as you mentioned, they added classes mainly as syntactic sugar.

en.wikipedia.org/wiki/Object-orien...

You have objects, they can encapsulate state, they receive messages from the outside using their public interface at runtime, that's more or less all there is for a basic OO language.

If you look closely at Python source code you'll notice how one in theory could implement object orientation directly in C, this is the constructor of a Python object:

PyObject *
_PyObject_New(PyTypeObject *tp)
{
    PyObject *op;
    op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
    if (op == NULL)
        return PyErr_NoMemory();
    return PyObject_INIT(op, tp);
}

from github.com/python/cpython/blob/014...

True, I got ahead of myself. Prototypal Inheritance is different than class inheritance, but both are OOO. The use of classes in Javascript with their introduction in ES6, has helped many developers get over this initial difference. But as you said, it really is just syntactical sugar over the same ole' prototypal inheritance chain.

 

Use cases to use classes and OOP are rare in web programming. I mostly find my self creating a class, just to organize a set of functionalities, which are otherwise can be written as simple functions & put together as module.

While writing query builders, I had used classes & inheritance, where there are some low level methods which can be used from all the inheriting classes & inheriting classes would provide their functionalities using the low level methods.

Disclaimer: The code that I write are very simple ones.

 

Use cases to use classes and OOP are rare in web programming

If we are only talking about JS, I agree - but web programming also covers PHP, Ruby, Java, Python and many others, all of which are very OOP and utilize classes for almost everything.

Having said that, I too have written classes in JS, just to break it apart again later into simpler modules.

 

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);
   }
}
 
 

Symbols, I still don't quite get how to use them.

 
 

It's a cool party trick but why, why is this a thing?

According to a JS expert the main usage is to create unique property keys and unique constants.

 

Coming from a Java background the concept of 'this' was the hardest part for me to understand.

 
 

Yeah there is, but there is a little bit different between the two languages

 

for me it was Promises, it was kind of difficult to understand it at first, but after practicing it, I realized that the principle around it is not that difficult to understand.

 

For me it was prototypal inheritance and how different it is from class based inheritance

 

promises, and then observables!
I can't say I have tamed aby of them, yet...

 

For me right now, its array and objects manipulation. Its kinda tricky for me when getting data from a third party API and want to structure it like the way i want πŸ™πŸΌ

 
Classic DEV Post from Apr 3

Workspace Wednesday: Show off your desk/computer setup!

Let’s share pics of everybody’s setups. Feel free to add details about your ha...

Nick Shoup profile image
GIS Developer, outdoor enthusiast, cryptocurrency/blockchain enthusiast, father, and husband. I really just want to share my passion for coding with others.