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

  • Home
  • SEARCH
  • 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 5997041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:12:23+00:00 2026-05-23T00:12:23+00:00

I have a generic function for XMLHttpRequests. as follows: function XMLRequest(address, data){ this.html =

  • 0

I have a generic function for XMLHttpRequests. as follows:

function XMLRequest(address, data){
    this.html = "null";
    this.xml = null;
    this.stat = null;
    req = new XMLHttpRequest();

    req.addEventListener('readystatechange', function(this) {

        console.log("from event listener, this.html = "+this.html);
        this.ready = req.readyState;
        if(req.readyState == 4)
        {
            this.stat = req.readyState;
            if(req.status == 200)
            {
                try{
                    this.xml = req.responseXML.documentElement;
                }
                catch(err){
                    this.xml = err;
                }

                try{
                    this.html = req.responseText;
                }catch(err){
                    this.html = err;
                }
                console.log("html = "+this.html);
            }
        }   
    }, false);

    req.open("GET", address+"?"+data, true);
    req.setRequestHeader('content-type', "text/xml");
    req.send();    
    return this;

};

I made it with the idea that i would use the watch() function to watch for a change in in this.html or this.xml. However, I never saw the change, and I realized why;

Inside of the anonymous listener function, this refers to the anonymous function, not XMLRequest

I was hoping to find a way around this, to somehow get the xml & text response out of the listener function and into the this.xml & this.html of the XMLRequest function, so that I can then watch for it

Is there any way I can do that?

–edit–

Brad’s example, with my edits, for discussion:

function MyObject(){
    this.publicFoo = 'BAR';
    var privateFoo = 'bar';

    var self = this; // store current context so we can reference it in other scopes.
    this.CallMe = function(){
        self.publicFoo = 'newBAR';
        setTimeout(function(){
            alert('public: ' + self.publicFoo + '\nprivate: ' + privateFoo);
        },100);
    };

    return this;
};

var myobj = MyObject();
myobj.CallMe();
alert(myobj.publicFoo)  <-- I want this to alert "newBAR", not "BAR"
  • 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-23T00:12:23+00:00Added an answer on May 23, 2026 at 12:12 am

    Store the variables you need before using them, then reference them (as you’d typically do to avoid scope problems:

    function MyObject(){
        this.publicFoo = 'BAR';
        var privateFoo = 'bar';
    
        this.CallMe = function(){
            // set them as something referenceable when not within the object's scope
            var iSee_publicFoo = this.publicFoo,
                iSee_privateFoo = privateFoo;
    
            setTimeout(function(){
                alert('public: ' + iSee_publicFoo + '\nprivate: ' + iSee_privateFoo);
            },100);
        };
    
        return this;
    };
    
    var myobj = MyObject();
    myobj.CallMe();
    

    Using the var self = this version:

    function MyObject(){
        this.publicFoo = 'BAR';
        var privateFoo = 'bar';
    
        var self = this; // store current context so we can reference it in other scopes.
        this.CallMe = function(){
            setTimeout(function(){
                alert('public: ' + self.publicFoo + '\nprivate: ' + privateFoo);
            },100);
        };
    
        return this;
    };
    
    var myobj = MyObject();
    myobj.CallMe();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain this behavior in Generics? I have a generic function in C#
If you have this generic function: template<class type, class ret, class atype1, class atype2,
I have made a generic function to show jQuery dialogs in my app. this
I have a web service which has a generic function that returns a dataset
I have a generic method with this (dummy) code (yes I'm aware IList has
I have a generic function: void ImageAlbum::ExpressButtonPressed( boost::function< void ( thumb::PhotoPrintThumbnail*, thumb::PhotoPrintFormat, thumb::PhotoPrintQuantity )
I have a generic function that returns URLs. (It's a plugin function that returns
I have a generic function that prints exceptions (using log4j): private void _showErrorMessage(Exception e)
I have a generic function that is constrained to struct. My inputs are boxed
I want to have a generic function for a few types such as long,

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.