Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!