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

155
Views
Is there a way to reference "the current class" from a Javascript class method

I'm wondering if there's a way to reference the "current class" rather than a class by name, from within a Javascript class.

For example:

class MyBase{
    static makeNew(id){
        const newInstance = new --currentClass--;// magic happens here
        newInstance.id = id;
        return newInstance;
    }
}

class A extends MyBase{}
class B extends MyBase{}

const newA = A.makeNew(1);
const newB = B.makeNew(379);

Is there a way for me to write MyBase::makeNew in such a way that when it's called from the A class, it returns a new instance of A, but when called from the B class, it returns a new instance of B?

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

0

Since the call signatures are like:

A.makeNew(1);
B.makeNew(379);

That might look familiar - you can use this to reference the object it's being called on.

class MyBase{
    static makeNew(id){
        const newInstance = new this();
        newInstance.id = id;
        return newInstance;
    }
}

class A extends MyBase{}
class B extends MyBase{}

const newA = A.makeNew(1);
const newB = B.makeNew(379);

console.log(newA instanceof A);
console.log(newB instanceof B);

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!