0

I have a super class called API in a separate file called API.js

class API {
???
}

I also have a bunch of subclasses each in a separate file (MySubClassXXX.js) that include a number of static methods like:

class MySubClass1  {
     static method1 () {
        return { something }
     }
     static method2 () {
        return { something else }
     }
}

I would like yo use these methods in my project like this API.MySubClass1.method1()

how should I import and define the subclasses MySubClass1 in the main class API

3
  • Do these only include static methods? Commented Sep 20, 2021 at 8:11
  • yes they do. only static Commented Sep 20, 2021 at 8:45
  • 1
    Then you should not be using classes in the first place. Use object literals. Or instances of a base class if you want to share some of the methods and overwrite others. Commented Sep 20, 2021 at 9:09

1 Answer 1

2

You can assign a subclass definition to a property of the base class. class declarations in Javascript are first-class values.

class API {}

API.MySubClass1 = class {
  static method1() {
    return 'something'
  }
}

console.log(API.MySubClass1.method1())

If you only intend to use classes as namespaces here, you can also safely drop class and static keywords with the same effect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.