Error while accessing to a variable

I have a problem while trying to access to a variable with my xmlhttprequest. I have the following code :

function MyObject(){
    this.variable = 0;
}

MyObject.prototype = {
    request: function(url, call_function){
        try{
            if(window.XMLHttpRequest)
                httpRequest = new XMLHttpRequest();
            else if(window.ActiveXObject)
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

            httpRequest.onreadystatechange = function(data)
            {
                try
                {
                    if(httpRequest.readyState == 4)
                    {
                        if(httpRequest.status == 200){
                            var tab = JSON.parse(httpRequest.responseText).childs;
                            call_function.apply(this, Array(tab));
                        } 
                    }
                }
                catch(e){}
            };
            httpRequest.open('GET', url);
            httpRequest.send();
        }
        catch(err){}
    },

    start: function(url){
        this.request(url, this.func);
    },

    func: function(){
        try{this.variable = 5;}
        catch(err){alert(err);}
    }
};

var obj = new MyObject();
obj.start(url);

With this code, when the programs executes the "func" function it catches an exception and it tells me that "this.variable" is not defined. Do you know why I can't access to this property ?

Answers


When you call

call_function.apply(this, Array(tab));

this doesn't refer to your MyObject instanciated. It refers to the function you declared and assign to the onreadystatechange event. You need to save the reference to the object :

request: function(url, call_function){
    try{
        var self = this;
        if(window.XMLHttpRequest)
            httpRequest = new XMLHttpRequest();
        else if(window.ActiveXObject)
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

        httpRequest.onreadystatechange = function(data)
        {
            try
            {
                if(httpRequest.readyState == 4)
                {
                    if(httpRequest.status == 200){
                        var tab = JSON.parse(httpRequest.responseText).childs;
                        call_function.apply(self, Array(tab));
                    } 
                }
            }
            catch(e){}
        };
        httpRequest.open('GET', url);
        httpRequest.send();
    }
    catch(err){}
},

Need Your Help

What transformations are done to requests sent through fiddler?

asp.net fiddler heisenbug

I am currently working on a reverse proxy project done in ASP.Net. It receives requests from a client, creates an HttpWebRequest object to another server (with the same HTTP headers), reads the res...

Table-Layout: have one row get all space required (because of rowspan)

html css html-table tablelayout

I havea few complex data-table and am facing a problem with they layout: there is one cell wich gets a rowspan across all other rows, but the content of that cell is variable in height and may well

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.