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 8335807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:47:04+00:00 2026-06-09T03:47:04+00:00

I am trying to set a variable value at an object (class) level from

  • 0

I am trying to set a variable value at an object (class) level from deep inside a few levels of nested anonymous javascript method calls. How do I do this?

Here is some code to explain what I am trying to do. Disclaimer: I am not that comfortable with the concept of closures in javascript, so I might be going along a wrong path here. Any suggestion about a succinct way to achieve what I want to do would be greatly appreciated.

// FileUtils object.
var FileUtils = function () {
    // Member variables.
    this.ConfRootDir = null;
};

// Method to get a file entry.
// successCallback has to be a method with a FileEntry object.
FileUtils.prototype.getFileEntry = function (fileName, successCallback) {
    if (this.ConfRootDir == null) {
        var thisObj = this;
        // Request the root filesystem 
            // [** 1st callback, using anon method]
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            function (fileSystem) {                        
                // Get the root directory of the file system.
                var rootDir = fileSystem.root;
                // Get the ConferenceToGo directory, or create if required.
                // [** 2nd callback, using anon method]
                rootDir.getDirectory("ConferenceToGo", { create: true, exclusive: false },
                    function (confDir) {
                        // Set values for future use 
                        // [** Definitely wrong scoping. The class level variable 
                        // can't be accessed using 'this'. What to do? **]
                        this.ConfRootDir = confDir;
                        // Now try getting the handle for the list file.
                        //  [** 3rd callback, using anon method. Irrelevant at this point.]
                        this.ConfRootDir.getFile(fileName, { create: false },
                            successCallback, // Success callback [getFile]
                            function (error) {
                                logError("Unable to retrieve file: ", true, true, error);
                            }); // Failure callback [getFile]
                    }, // Success callback [getDirectory]
                    function (error) { logError("Unable to create new directory: ", true, true, error); }); // Failure callback [getDirectory]
            }, // Success callback [requestFileSystem]
            function (error) { logError("Problem reading file system: ", true, true, error); }
        );
    }
}

I know that the scoping (by using ‘this’) is all wrong in the above piece of code, but not sure how to get it right. I have seen a few answers about binding to context (like this one), but I am using anonymous methods so that makes it harder. Note: Although I show only one method in the prototype of FileUtils here, there a few more.

Those who are aware can probably recognize that I am using methods from the cordova (PhoneGap) library for cross-platform mobile dev in HTML5 and JS, but that is not really much relevant here.

  • 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-06-09T03:47:06+00:00Added an answer on June 9, 2026 at 3:47 am
    … function() { function() { function() { …
                        // Set values for future use 
                        // [** Definitely wrong scoping. The class level variable 
                        // can't be accessed using 'this'. What to do? **]
                        this.ConfRootDir = confDir;
    

    You already have preparated the answer: thisObj.ConfRootDir. The thisObj variable is available in the scope of the nested function, and still points to the this keyword of the outer getFileEntry function, i.e. to the FileUtils instance.

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

Sidebar

Related Questions

I'm trying to set a static pointer variable in a class but I'm getting
I'm trying to set the variable of of the for statement to another variable.
I'm trying to set a variable in my local.xml file for my custom block:
Well I'm trying to set a variable to use in a thread, it works
I am trying to set the CLASSPATH variable so that my java programs can
I am trying to set and catch the URL variable It begins with a
I'm trying to figure out Afterthought. I want to set the variable HasChanged when
I am trying to access an instance variable which is set in the controller
I am trying to use variable in different function, I want to set global
I have an Objective-C view controller class, from which I am trying to call

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.