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

142
Views
Call Class Constructor as Function on Instance

I am writing an object pool class that holds onto old unused objects, and when a new one is needed can use one of its reserves (Without the expensive creation of objects). However, I want to be able to save any data to this pool (not limited to something such as extends Poolable). The problem is that most object pools require something like this:

class MyData {
    constructor(str) {
        this.reset(str)
    } 
    reset(str) {
        this.str = str;
    }
}

So that when a new instance is needed, the pool can call oldInstance.reset(prams). As stated before, I do not want this (Working with lots of third party tools that I am not feeling like writing wrappers for), so my data looks like this:

class MyData {
    constructor(str) {
        this.str = str;
    } 
}

When the pool needs to reset an instance, I need to be able to call the constructor as a function and set the this value to the instance that is being wiped.

This is super easy with old classes that used function/prototype syntax when they were made:

const MyClass = function(str) {
    this.str = str;
}

// Reset an instance
const instance = new MyClass("foo");
MyClass.apply(instance, ["bar"])
// done

However when I do that for classes, it complains that you can not use a class without the new keyword. How do I go about doing this?

Fiddle: https://jsfiddle.net/wuqek405/

Edit: As I said, most object pools need a reset function. I am trying to use the constructor as this function, because it is supposed to “set up” the instance. Another solution would be to generate this reset function based on the class. However, I want it to be as fast as possible, so terribly hacky solutions such as stringifying the class and evaling the constructor wouldn’t be optimal.

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

0

This is not possible. A class is not callable1, only constructable - which means creating a new object. This was new in ES6, which overhauled the inheritance model of classes and introduced super. Also, ES2022 will introduce class fields, which also get created during construction without being mentioned in the constructor code.

Your only option is to use only ES5 function-based classes, a transpiler, or writing explicit reset methods.

1: technically, it is callable (typeof C == 'function'), but [[Call]] will always throw an exception

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!