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

207
Visualizações
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 à 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