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

204
Vistas
If a class calls some outside function then how to access this class reference in that outside function

Consider the following code snippet.

function outsiderFunction(opt){
    opt.helperMethod();
}

class SomeClass {
    constructor(options) {
        this.options = options;
    }
    update() {
        outsiderFunction({
            helperMethod: this.help
        })
    }
    help() {
        this.helpUtil();
    }
    helpUtil() {
        console.log('help util')
    }
}
const x = new SomeClass();
x.update();

The update function of SomeClass calls the outsiderFunction passing the helperMethod as one of the functions (help) of the same class.

But now the help function also needs to call one of the functions of that class named helpUtil but since it was called by an outsider function the this reference refers to outsider function and not the class.

Note: I can not bring outsider function inside the class

One workaround kind of thing that I did is as shown below:

function outsiderFunction(opt){
    opt.helperMethod();
}

class SomeClass {
    constructor(options) {
        this.options = options;
    }
    update() {
        outsiderFunction({
            helperMethod: this.help,
            ReferenceClass: this
        })
    }
    help() {
        let that  = this.ReferenceClass;
        that.helpUtil();
    }
    helpUtil() {
        console.log('help util')
    }
}
const x = new SomeClass();
x.update();

Now, I want to know if there is a better way to handle this situation as the workaround done does not seems to be a good practice.

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