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

213
Vistas
Calling functions of a class via apply doesn't initialize the values in the constructor

I have javascript classes in multiple files. I would like to call a function from a given class by only supplying their names as arguments.

class Index{
    constructor(){
        this.Loader("test", "TestFunc");
    }

    Loader(cname, fname){
        const template = require(`./${cname}.js`);
        const object = new template();
        object[fname].apply();
    }
}

new Index();
module.exports = class Test{
    constructor(){
        this.text = "this text should appear";
    }

    TestFunc(){
        console.log(this.text);
    }
}

When the instance of the Test class gets initialized the constructor will run and prepare the value, but when I call TestFunc via the apply function the same values appear undefined and the error "Cannot read property text of undefined" gets thrown. When calling the function normally via object.TestFunc(); no such error is present.

about 4 years ago · Juan Pablo Isaza
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