• Isadora
  • Get it
  • Forum
  • Help
  • ADD-ONS
  • Newsletter
  • Impressum
  • Dsgvo
  • Impressum
Forum
    • Categories
    • Recent
    • Popular
    • Tags
    • Register
    • Login

    [ANSWERED] Class Definitions in Javascript

    How To... ?
    2
    2
    88
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      GaryGalbraith
      last edited by Woland

      I am needing to create an array of objects within my Javascript Actor and am having difficulties in creating the necessary class definition

      When I do the following:

      class Test{}

      function main()
      {return 0;}

      I get a compiler error saying that "Test has already been declared". However, if I do the following:

      function main() {

      class Test{}

      return 0;}

      (putting the class definition within the body of the main function) then it works just fine.

      I have not found any documentation that specifies that class definitions have to be within a function.  Eventually I will need to develop different class structures and would want to put these definitions in an external .JS file

      Any thoughts/suggestions?

      Izzy 3.2.6 Windows 11 Pro 64-bit, 13th Gen Intel Core i9-13900H 2.60 GHz, 64GB RAM, 2TB SSD, NVIDIA GeForce GTX 4070

      DusXD 1 Reply Last reply Reply Quote 0
      • DusXD
        DusX Tech Staff @GaryGalbraith
        last edited by DusX

        @garygalbraith

        Using namespaces for your Classes should allow you to create the modular structure you are after.
        Try defining your Class like:

        var MyApp = MyApp || {};
        MyApp.MyClass = class {
          constructor(param1, param2) {
            this.param1 = param1;
            this.param2 = param2;
          }
          method1() {
            this.param1 = this.param1 + this.param1;
          }
          method2() {
            this.param2 = this.param2 + this.param2;
          }
        }
        

        Then create an instance in main() like:

        const myInstance = new MyApp.MyClass(3, 5);

        This should allow you to name each class uniquely and include them from external files.

        Troikatronix Technical Support

        • New Support Ticket Link: https://support.troikatronix.com/support/tickets/new
        • Isadora Add-ons: https://troikatronix.com/add-ons/
        • My Add-ons: https://troikatronix.com/add-ons/?u=dusx

        Running: Win 11 64bit, i7, M.2 PCIe SSD's, 32gb DDR4, nVidia GTX 4070 | located in Ontario Canada.

        1 Reply Last reply Reply Quote 2
        • First post
          Last post