Discord
Login
Community
DARK THEME

[JavaScript] Classes defined in other files cannot be instantiated in main

Problem is as stated above. If I define classes on one file, they cannot be instantiated in another file. For instance, say I have a class called MyClass defined in the file "classes"

And in init() in main, I have something like the following: myObject = new MyClass()

I would get the error MyClass is not defined, in file "main" at line x

This makes it difficult to keep things organized.

Try defining your class with:

MyClass = class {
  ...
}

Instead of:

class MyClass {
  ...
}

This should solve your problem.

Also if you need to extend a class which is defined in another file, you should look into a solution I had previously suggested here: https://microstudio.dev/community/questions/javascript-how-to-reference-objects-from-other-files/280/2/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community