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

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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