Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
use a static method from a sub class in javascript

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!