Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda