Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3956710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:23:54+00:00 2026-05-20T02:23:54+00:00

I have JS code roughly like this: function myObject() { this.a = 13; this.fetchData

  • 0

I have JS code roughly like this:

function myObject()
{
    this.a = 13;

    this.fetchData = function()
    {
        alert(this.a);
        getData(this.processData);
    }

    this.processData = function(data)
    {
        // do stuff with data
        alert(this.a);
    }

    this.fetchData();
}

function getData(callback)
{
    // do async request for data and call callback with the result
}

My problem is: The function fetchData has access to my a variable via the this keyword, but the other function processData does not when called by getData. I understand why this happens, but don’t know how to work around it.

How would you approach this problem preferably in OOP style? (The function getData has to be available to multiple classes)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:23:54+00:00Added an answer on May 20, 2026 at 2:23 am

    Two options:

    1) Have getData accept a context parameter (usually called context or thisArg) and use callback.apply(context, ...) or callback.call(context, ...) to call it. So:

    function getData(callback, context) {
        // ...when it's time to call it:
        callback.call(context, arg1, arg2);
        // or
        callback.apply(context, [arg1, arg2]);
    }
    

    2) Create a function that, when called, will turn around and call the original callback with this set to the correct value. (This is sometimes called “binding”.)

    For example, using an explicit closure:

    this.fetchData = function()
    {
        var self = this;
    
        alert(this.a);
        getData(getDataCallback);
    
        function getDataCallback(arg1, arg2) {
            self.processData(arg1, arg2);
        }
    }
    

    Or have a generic bind function to do it (which will also involve a closure, but in a nice controlled context so it’s not closing over stuff you don’t need). See link below for an example of a simple bind function.

    More: You must remember this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code which looks roughly like this: String urlString = ftp://polar.ncep.noaa.gov/pub/history/waves/multi_1.glo_30m.dp.200601.grb2;
I have code like this in my view model: function ChatListViewModel(chats) { var self
I have some PHP code which looks roughly like this require_once 'cache.php'; require_once 'generator.php';
Straight into business: I have code looking roughly like this: char* assemble(int param) {
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
I have this code, and for some reason my $.post function fires twice in
I Have an MVC Project roughly organized like this: Project 1: MVC app (ViewModels,
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
All, I have a situation that looks roughly like this: My HTML page a
I have some Scala code roughly analogous to this: object Foo { val thingA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.