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

132
Visualizações
Access this. of parent function within a function in JS

I think the question title sums it up pretty well, if not, let me explain:

I have a class, that one has a function and inside that function A I NEED to have another function (function B), which needs to access a variable of function A. I hope this is enough, I really can not find any better words of how to describe this issue.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you need arrow functions for that

class Some() {
    constructor() {
        /* ... */
    }
    A(params) {
        let localVariable = 1
        const B = () => {
            // here you can access to the context (this)
            // and to A local variables (params, localVariable )
        }
    }
}

If i understood you correctly. And please ask more specific questions next time. Also try to share your code. It would be easier to understand

about 4 years ago · Juan Pablo Isaza Relatório

0

This sounds like a design problem. It sounds like you're attempting to break variable scoping rules. It seems to me, that this is what you're trying to do:

class some_class {
    A_function() {
      let variable1 = 5;
    }
    B_function() {
      let variable1 = 10;
    }
}

'variable1' is defined in A_function and is not accessible to B_function because of scoping rules. In fact, the variable1 in A_function and the variable1 in B_function are different variables.

However, a better implementation, that would likely work as you intend would be more like this:

class some_class {
    constructor() {
        this.variable1 = 5;
    }
    A_function() {
        this.variable1 = 10;
    }
    B_function() {
        this.variable1 = 15;
    }
}

Now, this.variable1 is defined as a property of the class and is accessible to both functions. Either function can modify the value of the variable.

Is this what you have in mind?

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