Where are the Javascript classes before I use them?

Hello,

Now that I’ve started using P5js and therefore Javascript, I’m curious about how it works internally.:slight_smile:

So, an object in javascript is likely to be permanently erased from memory when there is nothing more that references it by the garbage collector.

Since there aren’t really any classes in JavaScript, when I use the keyword class, I necessarily create an object somewhere and when I “create an instance” with the keyword new, I create a new object with the constructor of the parent class (which is actually an object) and it reference the parent object that I defined with the class keyword (via its prototype,



 __proto__ of the new object = the object created with the class keyword

Is that correct?

So, the funny question I ask myself is:

How does JavaScript make sure not to delete the objects created with the class keyword before I use them to create new objects with the new keyword (the pseudo instances)?

I suppose it’s referring to them somewhere so that the garbage collector doesn’t erase them???

N.B. I didn’t find the answer on internet … hum, I didn’t think to ask to an I.A. !!! I’ll try tomorrow !!!

Well, I’m answering my question since I asked it here (and I couldn’t wait to ask Gemini)

the class keyword behaves like the let keyword, that is, it creates a new variable in the scope with the class name

so the garbage collector isn’t going to delete it !

:slight_smile:

and so, I suppose one must not create a variable with the name of a class (in the same scope) !

Keyword class, just like let, const, var, function, import and using, is used to declare a variable in JS.

Basically, class is a constructor function which returns an object instance when invoked via keyword new.

However, just like any JS object (functions & arrays are objects too), the class block itself can become eligible to be garbage-collected once it’s outta scope.

But there are some things that will withhold its fully deletion:

  1. As long as any instance object of it exists, the class can’t be deleted; b/c the instance’s __proto__ points to the class.

  2. As long as any variable, closure, property or array is still storing the class itself.

  3. The class was declared globally at top-level scope.