Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda